www to non www redirect nginx code example
Example 1: nginx redirect www to non-www
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
Example 2: nginx redirect to non www
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
Example 3: redirect www to non-www nginx
# With wildcard:
server {
server_name ~^(www\.)(?<domain>.+)$;
return 301 $scheme://$domain;
}