rewrite rules - Nginx Wildcard SSL with Wordpress Multisite Subdomains

admin2025-06-02  2

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)
}
Share Improve this question asked Mar 11, 2019 at 21:04 ElkratElkrat 1481 silver badge9 bronze badges 2
  • hmmm is there a WP specific component you're unsure of? It might be better moving this to stackoverflow – Tom J Nowell Commented Mar 11, 2019 at 21:15
  • You're right. I drifted away from a WP-specific issue in this case. – Elkrat Commented Mar 13, 2019 at 2:57
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748817607a313973.html

最新回复(0)