Redirect additional domains to primary domain

I have a primary domain name ‘www.mydomain.org.uk’, and I want to redirect my additional domain name ‘www.mydomain.com’ to that, whilst preserving the server relative path.

Can anyone explain how to do this? Is it perhaps achieved by a multiple value for site_hosts in wordpress_sites.yml? It’s not immediately obvious from the documentation.

This is best accomplished by modifying your DNS records and isn’t related to Roots.

Hi @cfx - perhaps I should be more specific:

Let’s say I’ve already got all my desired domain names pointing at the IP address of my server. This might include records for both www and no-www domains.

Normally, the next thing I would do would be to add a redirect to my .htaccess, pointing all additional domains to the primary domain. This would look like this:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.mydomain.org.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.org.uk/$1 [L,R=301]

In other words, anything that isn’t my primary domain should be redirected to my primary domain (preserving the server-relative path).

My question is, is there anything in Trellis that does this for me, or should I be using my normal method above?

Update: I’m looking into a way of setting up the redirects within the nginx conf file, instead of a htaccess.

This is going to involve editing trellis/roles/wordpress-setup/templates/wordpress-site.conf.j2. The logic will be that if there is more than one value in site_hosts, then additional hosts will be redirected to the first host.

@cfx - I appreciate that initially this may not have looked like a Trellis related question, but would you now consider listing it in the forum again? I would appreciate any help, and I think the answer could be useful to other users.

Trellis doesn’t automatically set up redirects for site_hosts. It just automatically sets up the Nginx site to serve those hosts as well (but without redirects).

I suggest creating a simple Nginx conf for the redirects and using Nginx Includes.

For posterity:
As of roots/trellis#622, site_hosts facilitates automatic redirects (configuration example).

1 Like

Where to create Nginx includes please provide me a guide line please.

For redirecting domains, see: https://roots.io/docs/trellis/master/wordpress-sites/#options

If you really need to use custom Nginx config, see: https://roots.io/docs/trellis/master/nginx-includes/#include-files