3

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;
}
cnst
  • 14,646
physalis
  • 145

2 Answers2

4

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;
}
cnst
  • 14,646
-1

TRY THIS~

server { listen 80; server_name example.com; charset utf-8; access_log logs/xxxxxx.access.log;

root   /var/www/html;

index index.php;
location = / { return 301 /cn/; }

location / {
    try_files $uri $uri/ /cn/index.php?q=$uri;
}
location /en {
    try_files $uri $uri/ /en/index.php?q=$uri;
}
location /my {
    try_files $uri $uri/ /my/index.php?q=$uri;
}
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass   127.0.0.1:9000;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
}

}