Last Updated: February 25, 2016
·
659
· himself

Nginx no-www to www and www to no-www

If you want your website URL with the scheme www.domain.com redirects to domain.com in NGINX

Strip www from domain name with NGINX 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#
}

Add www to domain name with NGINX Redirect

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

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

As you can note, this is exactly the opposite, and will not bring SEO mark downs, as it is a complete redirect and move. The no-www way is forced and the directory shown.