nginx sub directory wildcard rewrite

6,683

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;
}
Share:
6,683

Related videos on Youtube

physalis
Author by

physalis

I am a (mostly web) designer and front-end coder specialised in WordPress and all its goodness. I believe in the might of CSS and will fight it with my life :). Still learning PHP and JS on a pure what-I-just-need basis. Whenever I can, I spend my time on making music, alone or with friends as we go along ;).

Updated on September 18, 2022

Comments

  • physalis
    physalis almost 2 years

    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;
    }
    
    • gxx
      gxx almost 8 years
      Isn't location / { [...] } sufficient?
    • physalis
      physalis almost 8 years
      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.
    • gxx
      gxx almost 8 years
      Hm...could you please show the debug log of a request while using location / [...]?
    • gxx
      gxx almost 8 years
      "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?
    • physalis
      physalis almost 8 years
      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.
    • Tero Kilkanen
      Tero Kilkanen almost 8 years
      See my answer on a related question serverfault.com/questions/586586/…. Instead of proxy_pass and proxy_set_header, use try_files / index directives. The point there is using regular expression to capture a part of the URI into a variable and using that variable later in try_files.
  • physalis
    physalis almost 8 years
    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
    physalis almost 8 years
    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.
  • Congmin
    Congmin almost 8 years
    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. ;)
  • physalis
    physalis almost 8 years
    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!
  • Congmin
    Congmin almost 8 years
    @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.
  • physalis
    physalis almost 8 years
    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 :).