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;
  • Isn't location / { [...] } sufficient? – gf_ Jul 1 '16 at 1:02
  • Unfortunately, no. If I only use it that way it ignores the directive for all sub-directories. E.g. any index.php arguments in childfolder1, childfolder2 are being ignored, leading to 404. – physalis Jul 1 '16 at 1:04
  • Hm...could you please show the debug log of a request while using location / [...]? – gf_ Jul 1 '16 at 1:07
  • "E.g. any index.php arguments in childfolder1, childfolder2 are being ignored, leading to 404." So the file is found, but the args aren't passed correctly? – gf_ Jul 1 '16 at 1:13
  • Sorry, I have to revert. The location /directive is unfortunately not working at all, except for the root directory itself. One of the sites worked after I had used the explicit child directory for location, and after I changed it to / I obviously didn't reload nginx. Now none are working with the default directive. – physalis Jul 1 '16 at 1:48

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;
  • Sorry for taking so long, but this is exactly what I needed. Now no matter what subdirectory, all links are working the way as intended. – physalis Jul 6 '16 at 21:18
  • Great, glad it works, thanks for accept! Also, I now +1'ed your question -- you now have a rep of 15, and are capable of doing +1 to other posts! Feel free to try on this one first. ;) – cnst Jul 6 '16 at 22:02
  • yes, thank you, already know that from the other stackoverflow network sites, and thus irrationally tried to +1 your answer before, hehe :). Thanks a bunch for your help! – physalis Jul 6 '16 at 22:53
  • @physalis, great, thanks! it's sad that so few users bother to even accept the answers to their own questions, let alone do the upvoting bit. – cnst Jul 6 '16 at 22:59
  • 1
    This place is really awesome - you can ask stupid questions, and if you behave and are clear enough, mostly get free (!) and friendly support. It’s only natural to show some little gratitude I believe :). – physalis Jul 7 '16 at 7:50


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;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;


  • 1
    Hi @johnsongoey, can you elaborate a bit on what the above means? I don't have directories of the names /en and /my? Also, since I am using HHVM, the php directive is possibly not needed/usable for me, as fastcgi works through HHVM. – physalis Jul 1 '16 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.