I used this:
server {
server_name "~^(?!www.).*" ;
return 301 $scheme://www.$host$request_uri;
}
but this redirects everything. I need to write exceptions for subdomains along with this.
I used this:
server {
server_name "~^(?!www.).*" ;
return 301 $scheme://www.$host$request_uri;
}
but this redirects everything. I need to write exceptions for subdomains along with this.
If the server block containing server_name "~^(?!www.).*"
is matching m.domain.com
, you obviously do not have another server block with a server_name m.domain.com
.
Rather than use complex regular expressions to redirect website names to a default server block, you could use the default server block to perform the redirect instead.
For example:
server {
listen 80 default_server;
return 301 http://www.domain.com/$request_uri;
}
server {
listen 80;
server_name www.domain.com m.domain.com subdomain.domain.com;
...
}
See this document for more details.