nginx rewrite rules for website and blog changes

June 11, 2013 4.6k views
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!
5 Answers
You can redirect www.domain.com/ to domain.com and www.domain.com/* to blog.domain.com - 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 www.domain.com to domain.com. 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 www.domain.com's server block)

rewrite ^/$ http://domain.com permanent break;
rewrite ^/main(.*)$ http://domain.com$1 permanent break;
rewrite ^(.*)$ http://blog.domain.com$1 permanent;

Make sure you reload nginx.

With this config:

- http://www.domain.com/ redirects to http://domain.com
- http://www.domain.com/main/something redirects to http://domain.com/something
- Everything else redirects to http://blog.domain.com/

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

# Rewrites
rewrite ^/main(.*)$ http://domain.com$1 permanent;
rewrite ^/blog/ http://blog.domain.com permanent;
rewrite ^/(.*)/$ http://blog.domain.com/$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! :]
Have another answer? Share your knowledge.