Nginx sub directory wildcard rewrite

directorynginxrewritewildcard

I have set up a server block on my nginx server, e.g. with domain testsite.com. I want to be able to install separate WordPress installations into direct child folders of the root folders, e.g. /var/www/html/testsite.com/childfolder1, /var/www/html/testsite.com/childfolder2 etc., so they can be reached by testsite.com/childfolder1, testsite.com/childfolder2 etc.

The manual way to create redirects would be to insert it like so:

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

and repeat it for every site to come. Using location / only covers the root directory.
Is there a way to create a (regex?) wildcard rule that says: "For each direct sub directory, apply this try_files command" (which is obviously always the same for WordPress, just the folder names change)?

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

Best Answer

I cannot attest whether what you want to do will work, but below is the conversion of your "pseudocode" into actual nginx configuration (and provided that a likewise copy-paste solution was working for you, this should continue working, too).

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