I have a wildcard certificate that secures *.example and I need to strip out the canonical www for all requests made for subdomains, eg: www.subdomain1.example => subdomain1.example
I reviewed this question:
But the first server block they suggest:
server {
server_name www.example;
return 301 $scheme://example$request_uri;
}
does not work for www.subdomain.example
How can I catch and return a scheme for www.*.example ?
I reviewed another question: 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 *.example;
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.conf;
include snippets/ssl-params.conf;
server_name example *.example;
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)
}
I have a wildcard certificate that secures *.example and I need to strip out the canonical www for all requests made for subdomains, eg: www.subdomain1.example => subdomain1.example
I reviewed this question: https://stackoverflow/questions/11323735/nginx-remove-www-and-respond-to-both
But the first server block they suggest:
server {
server_name www.example;
return 301 $scheme://example$request_uri;
}
does not work for www.subdomain.example
How can I catch and return a scheme for www.*.example ?
I reviewed another question: https://serverfault/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 *.example;
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.conf;
include snippets/ssl-params.conf;
server_name example *.example;
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)
}
You can use regular expressions in the server_name
directive, but wildcard names (e.g. *.example
) take precedence. See this document for details.
For example:
server {
listen [::]:80 ipv6only=off;
server_name ~^(www\.)?(?<name>(.+\.)?example\)$;
return 301 https://$name$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
include snippets/ssl-example.conf;
include snippets/ssl-params.conf;
server_name ~^www\.(?<name>(.+\.)?example\)$;
return 301 https://$name$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.conf;
include snippets/ssl-params.conf;
...
}
The first server
block matches http
requests to any subdomain, and redirects to the non-www variant using https
.
The second server
block matches https
requests to subdomains which begin with www.
and redirects to the non-www variant.
The third server
block does not need a server_name
directive (as it is the default server) and handles all https
requests to the main domain and non-www subdomains.