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

I am using nginx and want if user type a.com to access the web, it will be redirected to www.a.com, thanks.

share|improve this question

migrated from stackoverflow.com Feb 14 '11 at 1:44

This question came from our site for professional and enthusiast programmers.

1  
IMO, it should be www.a.com -> a.com , www is redundant, its already the web. –  Kent Fredric Feb 12 '11 at 15:47
    
I agree with Kent in that you should redirect from www to just example.com. –  Jacob Feb 14 '11 at 2:07
add comment

4 Answers 4

I suggest that you take a look at the HttpRewriteModule and implement one that redirects all requests to a.com to www.a.com, preserving the original path.

share|improve this answer
add comment

What about

server {
    server_name  a.com;
    rewrite ^(.*) http://www.a.com$1 permanent;
}

The permanent attribute makes nginx redirect by HTTP 301 instead of (I think) 302.

share|improve this answer
    
It can't because i have clf.a.com such domains already... –  Bin Chen Feb 14 '11 at 2:45
add comment

Based on the rewrite rule from "Converting rewrite rules", you can use the following rewrite rule:

server {
    listen       80;
    server_name  a.com;
    rewrite   ^  http://www.a.com$request_uri?;
}
share|improve this answer
add comment

By far the simplest way to do this is at the DNS level and have the web server accept requests for both. No need to stuff around with rewriting.

share|improve this answer
add comment

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.