nginx 301 redirect to subfolder on primary domain

Posted by 187j3x1 on Server Fault See other posts from Server Fault or by 187j3x1
Published on 2011-02-09T06:29:31Z Indexed on 2011/02/09 7:27 UTC
Read the original article Hit count: 596

Filed under:
|
|

hello there, sorry for my poor english. i just set up wordpress on my vps, so far its the only item on my site. there for seo reason, i think is better redirect all primary domain to the blog folder.

primary domain is example.com wordpress is at example.com/blog what i want is rewrite www.example.com and example.com to example.com/blog.

googled got some scripts, and make some change paste into nginx config file. here is:

#301 redirect www to non-www
server {
server_name  www.example.com;
location = / {
rewrite ^/(.*) http://example.com/$1 permanent;
}
}

#301 non-www to subfolder
server {
server_name  example.com;
location = / {
rewrite ^/(.*)  http://example.com/blog$1 permanent;
}
}

it works at some degree, successfully redirect to example.com/blog. the only problem is i get 404 not found error. then i only make nginx redirect www to example.com/blog. ok, this time i can access blog page. i know there is something wrong in the non-www to subfolder script. but do not how to fix it :(

© Server Fault or respective owner

Related posts about nginx

Related posts about Wordpress