nginx rewrite rules for website and blog changes

Posted June 11, 2013 8k views
I previously had my blog urls at it has now moved to The main website used to be at and it is now simply at the root 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!

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.

Submit an Answer
5 answers
You can redirect to and* to - are you okay with that?
I've tried that and it was not working for. I actually couldn't get anything to work other than the redirect 301 to redirect to I've tried rewrites and location rewrite blocks and none work. Is there an additional step to take to enable rewrites in nginx its like they are disabled or the module isn't installed?

Anybody else having redirect issues with stock nginx in the Ubuntu 12.04 32bit?
Try these rules:

(in's server block)

rewrite ^/$ permanent break;
rewrite ^/main(.*)$$1 permanent break;
rewrite ^(.*)$$1 permanent;

Make sure you reload nginx.

With this config:

- redirects to
- redirects to
- Everything else redirects to

Let me know how it works for you! :]
Thanks again, I used your example and got it working with this:

# Rewrites
rewrite ^/main(.*)$$1 permanent;
rewrite ^/blog/ permanent;
rewrite ^/(.*)/$$1/ permanent;

I wasn't using a if block so I'm pretty sure that's why I had to remove the break statements.
Awesome, glad you got it working! :]