heihachi88
10/24/2014 - 11:22 AM

Nginx redirect from WWW to NON-WWW

Nginx redirect from WWW to NON-WWW

# global www to non-www redirect

server {
        server_name "~^www\.(.*)$";
        return 301 $scheme://$1$request_uri;
}

# single domain / separate server_name with www in extra server directive

server {
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
}

# it should go in /etc/nginx/includes/www-to-nonwww
# then include it in one of your sites-available files at the bottom
include /etc/nginx/includes/www-to-nonwww