1

Environment

This environment is Nginx with Wordpress Multisite installed.

Question

I need to redirect the primary multisite domain (e.g. domain.com, www.domain.com) to another domain (e.g. domain-new.com, www.domain-new.com). But I don't want sub-folders to redirect. For example, the /wp-admin/ folders needs to NOT redirect, otherwise I'll be unable to administer the multisite network.

Obviously, I don't want any other domains to redirect. Just the one stated above.

I found instructions on how to do this in Apache, but I haven't been able to figure it out in Nginx.

What I came up with so far

I came across the following:

server {
  listen 80;
  server_name domain.com www.domain.com;
  rewrite ^ $scheme://www.domain-new.com$request_uri permanent;

But this will also redirect sub-folders of domain.com. I was to sure how to make it exclude wp-admin folder from the redirection.

I also came across this option:

location = / {
rewrite ^ http://domain-new.com/$uri permanent;    
}

But I couldn't find anything on whether the location command can include a domain, and if it can, how to ensure a specific sub-folder (wp-admin) is not included in the redirect?

0

If your nginx server block is handling multiple domains, the simplest solution would be to test the $host parameter within an if block. Also, change the rewrite statement so that it only matches a single URI.

For example:

if ($host = domain.com) {
    rewrite ^/$ https://new-domain.com permanent;
}

This is placed near the top of the existing server block.

  • (I deleted my prior two comments. At first I thought this didn't work, because it caused issues. But I discovered those issues were due to a missing ; in a prior line of code). This solution is working just fine. Thank you. – omega33 Sep 4 '17 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.