NGINX Redirection/Return with MultiSite

I have an NGINX server running WordPress MultiSite (sub-directories). Due to the way some sites were originally created, I need to create a redirect (or more correctly in NGINX terminology, a “return”:wink: on requests to a sub-domain (we’ll call it sub.domain.com) to goto my.domain.com/sub (which is managed as a sub-site in my WP multi-site).

From what I’ve read, I have two basic choices – hard-code it in the nginx config or use some regular expressions to translate the redirect. Either way, I can’t seem to get it it to work. NGINX responds to sub.domain.com but it always redirects to the main sub-site in the WP site. Any ideas? Here is a copy of the relevant NGINX config. We are running SSL for both the front and back end.

I’d appreciate any other feedback you see on the config below. If you think it would be helpful to see more configuration, let me know, but I think I have the parts that matter.

server {

server_name sub.domain.com;

return 301 https://my.domain.com/sub$request_uri; #would like this to be dynamic

}

map $uri $blogname{

~^(?P<blogpath>/[^/]+/)files/(.*) $blogpath ;

}

server {

listen 80;

return 301 https://$host$request_uri;

}

server {

listen 443 ssl;

server_name my.domain.com sub.domain.com;

root /var/www/wwwroot_domain;

index index.php;

  • Aaron
    • CTO

    Your example looks correct. Is the redirect working then it gets redirected back? That would be the case if you didn’t change your configs and db tables to work as a subdir install.

    You could probably make it dynamic with something like:

    server_name ~^(?P<sub>.+).domain.com$;
    return 301 https://my.domain.com/$sub$request_uri;

    (untested)

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.