Redirect www to non-www on Nginx

How to redirect the www.domain.com version of your website to domain.com version on Nginx?

Here is how. You basically have to make two server blocks. The block with your normal configuration should be the version you want, the version you want to redirect should have a simple rewrite rule alone.

For example, to redirect www.vidyut.net to vidyut.net, you set up your server blocks like this:
server {
server_name www.vidyut.net;
rewrite ^(.*) http://vidyut.net$1 permanent;
}

server {
server_name vidyut.net;
# Your
# normal
# server
# configuration
# goes
# here
}

Leave a Reply

Your email address will not be published. Required fields are marked *