I have a wildcard certificate that secures *.example.com and I need to strip out the canonical www for all requests made for subdomains, eg: www.subdomain1.example.com => subdomain1.example.com
I reviewed this question: https://stackoverflow.com/questions/11323735/nginx-remove-www-and-respond-to-both
But the first server block they suggest:
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
does not work for www.subdomain.example.com
How can I catch and return a scheme for www.*.example.com ?
I reviewed another question: https://serverfault.com/questions/249952/wildcard-vhosts-on-nginx in which they use regular expression to match the server name, but I'm not sure how to apply this to my situation.
Here is my current setup:
server {
listen [::]:80 ipv6only=off;
server_name example.com *.example.com;
return 301 https://$host$request_uri;
}
server {
# SSL configuration
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name example.com *.example.com;
root /usr/share/nginx/webroot;
index index.php index.html index.htm;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
#subdomain multi site with wp in 'wp' subdir
if (!-e $request_filename) {
# Redirect wp-* files/folders
rewrite ^(/[^/]+)?(/wp-.*) /wp/$2 last;
# Redirect other php files
rewrite ^(/[^/]+)?(/.*\.php) /wp/$2 last;
}
...(etc)
}