How to configure nested subfolder to subdomain Nginx redirects

  • Posted on March 24, 2014
  • finidAsked by finid

I’m trying to set up multiple subfolder to subdomain redirect like this:

<pre> location ~ ^/~([^/]+)/(.)$ { location ~ ^/~ask/(.)$ { rewrite ^(.*)$$1 permanent;

location ~ ^/~forum/(.)$ { rewrite ^(.)$$1 permanent;

location ~ ^/~qa/(.)$ { rewrite ^(.)$$1 permanent;
} } </pre>

The idea is that all requests to, and will be redirected to With the above code, only redirects from are working. What could be wrong with the rest and is there a better way to setting them up?

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

That’s not needed – you can replace all of the location blocks with the one rewrite rule I posted.

How would you modify the location line to match, or is that not needed with your form of the rewrite?

Try using the following rewrite rule instead, does that fix it? <br><pre>rewrite ^/(ask|forum|qa)/(.*)$$2 permanent;</pre>