To redirect a www
domain to the « root » domain with Nginx, one always prefers not to make any use of the if
syntax (because it's evil), and rather use two different server
directives for each rule :
server {
server_name www.domain.com;
return 301 $scheme://domain.com$request_uri;
}
server {
server_name domain.com;
# Much more cool stuff here
}
Of course a 301 is a permanent redirection
Then :
sudo service nginx restart
Obviously, we don't want to rekindle discussions over whether it's a good practice or not(see this article or these bunches of pro, cons and WTF).