Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I just set up Wordpress on my VPS, so far it's the only item on my site. For SEO purposes, I think it's better to redirect the primary domain to the blog folder.

So let's say the primary domain is example.com and Wordpress is at example.com/blog. I want to rewrite www.example.com and example.com to example.com/blog.

I Googled and got some scripts and made some changes to my nginx config file. My configuration 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, it successfully redirects to example.com/blog. The only problem is I get a 404 not found error. When I only make nginx redirect www to example.com/blog I can access the blog page successfully.

I know there is something wrong in the non-www to subfolder script, but do not how to fix it. Any suggestions?

share|improve this question

2 Answers 2

Try this:

#301 redirect www to non-www
server {
    server_name  www.example.com;
    # redirect all requests
    rewrite ^(.*) http://example.com$1 permanent;
}
server {
    server_name  example.com;
    # this only matches /, and not any other requested file
    location = / {
        rewrite ^ http://example.com/blog/ permanent;
    }

    # rest of configuration goes here...
}
share|improve this answer
    
im really appreciate your help, but still got 404 page not found error. still still is something wrong with redirect non-www to blog folder script. :((( –  187j3x1 Feb 10 '11 at 12:01
    
What's the address you're typing that's ending up in a 404? –  Mark Rose Feb 10 '11 at 15:22
    
got 404 when access example.com/blog. but i fingure out use php script to redirect example.com to example.com/blog. and redirect 'www'.example.com' to example.com with nginx. in this way, i can access the blog folder, but this is not an efficient way. :(( –  187j3x1 Feb 10 '11 at 16:58
    
A simpler solution is to not use /blog, and host the blog in the domain root. –  Mark Rose Feb 10 '11 at 18:04

Your question has two parts.

  1. Getting the rewrite of the server name.

  2. Getting the rewrite to the said directory.

As per 1. your rewrite rule is incorrect. There's no need to capture anything, you just pass the variable $request_uri that contains the request like the client sends it to Nginx.


server {
    server_name  www.example.com;
    # redirect all requests
    rewrite ^ http://example.com$request_uri? permanent;
}

For the second part use a much simpler rewrite like this:


server {
    server_name  example.com;
    # this only matches /, and not any other requested file
    location = / {
        rewrite ^.+ /blog permanent;
    }

# rest of configuration goes here...

}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.