$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>rewrite rules - Nginx Wildcard SSL with Wordpress Multisite Subdomains|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

rewrite rules - Nginx Wildcard SSL with Wordpress Multisite Subdomains

matteradmin9PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far