Question

nginx rewrite rules for website and blog changes

I previously had my blog urls at http://www.domain.com/category_name/post_name/ it has now moved to http://blog.domain.com/category_name/post_name/

The main website used to be at http://www.domain.com/main/ and it is now simply at the root http://domain.com.

I have separate sites-available conf files for the main site and the blog.

I have searched high and low and cannot find anything that seems to work to redirect the incoming urls?

I’m using Ubuntu 12.04 LTS, nginx version installed is 1.1.19

Any help would be greatly appreciated!


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.

Awesome, glad you got it working! :]

Thanks again, I used your example and got it working with this: <br> <br> # Rewrites <br> rewrite ^/main(.)$ http://domain.com$1 permanent; <br> rewrite ^/blog/ http://blog.domain.com permanent; <br> rewrite ^/(.)/$ http://blog.domain.com/$1/ permanent; <br> <br>I wasn’t using a if block so I’m pretty sure that’s why I had to remove the break statements.

Try these rules: <br> <br>(in www.domain.com’s server block) <br> <br>rewrite ^/$ http://domain.com permanent break; <br>rewrite ^/main(.)$ http://domain.com$1 permanent break; <br>rewrite ^(.)$ http://blog.domain.com$1 permanent; <br> <br>Make sure you reload nginx. <br> <br>With this config: <br> <br>- http://www.domain.com/ redirects to http://domain.com <br>- http://www.domain.com/main/something redirects to http://domain.com/something <br>- Everything else redirects to http://blog.domain.com/ <br> <br>Let me know how it works for you! :]