NGINX configuratuion for automatic pretty links for multiple wordpress sites in subdirectories

52 Views Asked by At

I have a dev server where I upload client websites, every WordPress site has its own DB and directory (every site is independent). I have one server block. My problem is with the pretty permalinks, this works for me:

    server_name dev.example.tld;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }

    location / {
            try_files $uri $uri/ =404;
            add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-$
            expires off;
    }

    location /site_1/ {
            index index.php;
            try_files $uri $uri/ /site_1/index.php?$args;
    }

    location /site_2/ {
            index index.php;
            try_files $uri $uri/ /site_2/index.php?$args;
    }

I've been searching for a way to do it one time instead of adding the location block for each site but no luck.

1

There are 1 best solutions below

2
On

You could change the location into a regular expression and capture the site name:

location ~ ^(?<site>/[^/]+) {
    try_files $uri $uri/ $site/index.php?$args;
}

Alternatively, accomplish the same effect by using a rewrite:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^(/[^/]+) $1/index.php last;
    return 404;
}