finid
By:
finid

How to configure nested subfolder to subdomain Nginx redirects

March 24, 2014 4k views
I'm trying to set up multiple subfolder to subdomain redirect like this:
location ~ ^/\~([^/]+)/(.*)$ {
  location ~ ^/\~ask/(.*)$ {
    rewrite ^(.*)$ http://forum.site.com$1 permanent;     
  }

  location ~ ^/\~forum/(.*)$ {
    rewrite ^(.*)$ http://forum.site.com$1 permanent;     
  }

  location ~ ^/\~qa/(.*)$ {
    rewrite ^(.*)$ http://forum.site.com$1 permanent;     
  }
}
The idea is that all requests to site.com/ask, site.com/forum and site.com/qa will be redirected to forum.site.com. With the above code, only redirects from site.com/ask are working. What could be wrong with the rest and is there a better way to setting them up?
3 Answers
Try using the following rewrite rule instead, does that fix it?
rewrite ^/(ask|forum|qa)/(.*)$ http://forum.site.com/$2 permanent;
How would you modify the *location* line to match, or is that not needed with your form of the *rewrite*?
That's not needed -- you can replace all of the location blocks with the one rewrite rule I posted.
Have another answer? Share your knowledge.