This question has been asked in various ways a million times. Still I can not find an answer that is generic enough to cover all cases.
All I want is to remove www
from any incoming URL and I want that applied to all my subdomains. Here is my nginx config that is otherwise working fine:
# redirect HTTP to HTTPS, no www
server {
listen 80;
listen [::]:80;
server_name ~^(?<www>www\.)(?<subdomain>.+)\.example\.com$;
# redirect
return 301 https://${subdomain}.example.com$request_uri;
}
#redirect HTTPS www to non-www
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name ~^(?<www>www\.)(?<subdomain>.+)\.example\.com$;
# ssl config...
# redirect
return 301 https://${subdomain}.example.com$request_uri;
}
#default server
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com *.example.com;
# other calls below this point include
# root dir
# ssl config
# security headers
# various location blocks, specific to my project
# error page
# error/access logs...
}
All these mappings are handled fine by the config above:
http://example.com
->https://example.com
http://client1.example.com
->https://client1.example.com
http://dev.client1.example.com
->https://dev.client1.example.com
http://stage.client1.example.com
->https://stage.client1.example.com
But these are NOT:
http://www.example.com
->https://example.com
http://www.client1.example.com
->https://client1.example.com
http://www.dev.client1.example.com
->https://dev.client1.example.com
Can you help me understand what I need to change in my config or how to debug this?