I see the Nginx HttpRewriteModule documentation has an example to rewrite a www-prefixed domain to a non-www-prefixed domain:
if ($host ~* www\\.(.*)) {
set $h
As noted in the Nginx documentation, you should avoid using the if directive in Nginx where possible, because as soon as you have an if
in your configuration your server needs to evaluate every single request to decide whether to match that if
or not.
A better solution would be multiple server directives.
server {
listen 80;
server_name website.com;
return 301 $scheme://www.website.com$request_uri;
}
server {
listen 80;
server_name www.website.com;
...
}
If you're trying to serve an SSL (HTTPS) enabled site, you got more or less three different options.
if
directive.There is also an option to use SNI, but I'm not sure this is fully supported as of now.