My goal is to redirect example.com into www.example.com without redirecting any subdomain to www.
This is what I have:
server {
listen 443;
server_name example.com;
return 301 https://www.$server_name$request_uri;
}
With this configuration every subdomain (ex: foo.example.com) gets redirected into www.example.com, not just the main one without subdomain (example.com), as I would like.
I tried cleaning cache (and also doing it from other browsers) with same results.
Edit:
The proof that the server_name is matching subdomains is that if I change the redirection url to: https://www.$host$request_uri then:
foo.example.com gets redirected to www.foo.example.com.