Redirect www to non www

February 12, 2014 14.1k views
How do I redirect http://www.example.com to http://example.com? I am using LEMP, Nginx, Wordpress. Qn 1: To which file should I add the below code to? server { server_name www.example.com; return 301 $scheme://example.com$request_uri; } I have a few files in mind (I followed your tutorials) 1) /etc/nginx/sites-available/wordpress 2) /etc/nginx/sites-available/default Qn 2: If I add the above code, must I still do the DNS part on CNAME (www and @)? Or can I skip this step?
8 Answers
I think you need to add www and @ CName record to make the domain catch www,

also, I believe you need to have each vhost like the following code:

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

server {
server_name domain.com;
#The rest of your configuration goes here#
}

for exp. look here: http://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www
Thanks for your reply! I got it to work by just by adding the CName record and without the server{...} portion.

But what I really want to know is by adding the server{....} portion, do I still need to do the CName record? Because I actually want to do it through this method - i.e. to redirect through server {...} portion.

If the answer is yes, I will know that such a method actually exists. and my next step would be to figure how to go about just adding the server{...} portion alone. Now I'm like a lost sheep. Because I have added CName record and it already works so i cant tell if the server {...} is actually working.

thanks in advance!
when you open your vhost: /etc/nginx/sites-available/wordpress

you will need to have server{ } twice. each block will have different things where the 2nd one carry the rest of the configurations. as I wrote for you above, it should be like that to redirect...

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

server {
server_name domain.com;
#The rest of your configuration goes here#
}

Adding CName record will allow your server to catch www only thus you can redirect from www to non www

so in anyways you will have to add CName if you didn't.

do the same thing for the other vhost if you want to redirect it like I wrote:

for /etc/nginx/sites-available/default:

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

server {
server_name domain2.com;
#The rest of your configuration goes here#
}
Can you pastebin your current virtualhost config? You will need to add a DNS record so the browser knows what IP it should connect to.
Thanks FMCB and Kamal. I managed to get it to work.

Steps I did were:
1) I added CNAME record (is this the DNS record you are referring to?)
2) I only added the 301 redirect to /etc/nginx/sites-available/default.
I left /etc/nginx/sites-available/wordpress untouched.
When I added the same code for both, I had an error. So I tried each files individually, and realised only the default file worked after I restarted nginx.

this is my pastebin, please help me check if the code is ok.
http://pastebin.com/8s6n59m2

Thanks a lot!
The config looks good. Glad it's working now! :]
Have another answer? Share your knowledge.