Nginx WordPress rewrite for multiple sub-folders

nginxrewriteWordpress

I am currently setting up our new server for our company, and we are developing alot of different apps, and right now we want to have all our wordpress apps under wp.domain.com/app1, wp.domain.com/app2, etc.

The reason the default isn't working is because we want to use the %postname% permalink for all apps.

I am currently managing by doing a rewrite for every single sub-folder, but I'd rather rewrite every single subfolder with one location block, so I don't have to edit the server block each time we upload a new application to our server.

There is also a possibility we upload to a sub-sub-directory, for example wp.domain.com/appgroup1/app3, wp.domain.com/appgroup1/app6, etc.

Here's my wp.domain.com config:

server {
  listen 80;

  root /usr/share/nginx/html/wp;
  index index.php index.html index.htm;
  server_name wp.domain.com;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
  }

  location ~ /(.*)/ {
    index index.php;
    try_files $uri $uri/ /$1/index.php?$args;
  }

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-cgi alone:
    #fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one

  location ~ /\.ht {
    deny all;
  }
}

Note that we don't have use a multi-site setup.

Best Answer

This is time to use regex location blocks :

location ~ /(app1|app2|app3|groupapp1(?:/(subapp1|subapp2|subapp3)))/ { 
    try_files $uri $uri/ /$1/index.php$is_args$args;
    [ ... ]
}