WordPress In Subdirectories Itself

Update: This article is updated for WordPress 3.5 multisite’s file-handling.

It is not possible to setup WordPress-Multisite with subdomains in a subdirectory. As soon as WordPress detects you are running another WordPress from example.com/folder rather than example.com/ – it will not ask you to choose between sub-directories and sub-domains.

But it is possible to setup a WordPress-Multisite with Subdirectories in a subdirectory itself. This is what I will be covering today. Most of the stuff is similar to my previous post.

Creating A WordPress Multisite Network:

If you haven’t done this yet, please check our guide here.

Nginx Configuration for WordPress-Multisite with Subdirectories in a subdirectory itself!

Below is the recommended Nginx configuration for WordPress Multisite using subdirectories setup.

Generally we replace example.com with your domain in our configurations. In this case, we will also replace wordpress with your folder/subdirectory name.

server {
        server_name example.com ;

        access_log   /var/log/nginx/example.com.access.log;
        error_log    /var/log/nginx/example.com.error.log debug;

        root /var/www/example.com/htdocs ;
        index index.php;

        if (!-e $request_filename) {
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;         
                rewrite ^/wordpress(/[^/]+)?(/wp-.*) /wordpress$2 last;      
                rewrite ^/wordpress(/[^/]+)?(/.*\.php)$ /wordpress$2 last;
        }

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

        location ~ \.php$ {
                try_files $uri /wordpress/index.php;
                include fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
        }

        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                access_log off; log_not_found off; expires max;
        }

        location = /robots.txt { access_log off; log_not_found off; }
        location ~ /\. { deny  all; access_log off; log_not_found off; }
}

For Multisite Networks Created with WordPress 3.4 or older: Please use this article to improve static-file handling. It alone can improve overall performance by 10x.

In the next article, we will add support for WP Super Cache for the above Nginx config. Read the complete series here.

Must Read: Checklist For Perfect WordPress-Nginx Setup

14 Responses to “WordPress In Subdirectories Itself”

  1. himanshu

    How can I serve new WordPress installs from subdirectories? for example, I want to set up example.com/member/wordpress1
    examplec.com/member/wordpress2
    examplec.com/member/wordpress3

    I don’t want to use multi site feature at all, but install wordpress as a fully functional site in subdirectories.

    Thanks in advance and all these tutorials.

  2. Rahul Bansal

    You can try something like…

      set $dir "";
    
      if ($request_uri ~ ^/([^/]*)/.*$ ) {
            set $dir1 /$1;
      }
    
        location / {
            try_files $uri $uri/  $dir1/index.php?$args;
        }
    

    This is what we are using on demo.rtcamp.com where we have separate wordpress in subdir.

    As long as we add a new wordpress in top-level folder, above config can handle it automatically.

    • himanshu

      Thanks but after implementing this config, I start getting errors -File not found on all page,post links, even on the main site. What am I doing wrong here?

      I want to install WP like this – mysite.com/member/WPinstall1 and not mysite.com/WPinstall1

      Thanks once again.

      Below is my complete config:

      server {
              server_name mysite.com www.mysite.com;
      
          access_log   /var/log/nginx/mysite.com.access.log;
          error_log    /var/log/nginx/mysite.com.error.log;
      
          root /var/www/mysite.com/htdocs;
          index index.php;
      
          set $dir "";
      
          if ($request_uri ~ ^/([^/]*)/.*$ ) {
                set $dir1 /$1;
          }
      
          location / {
                  try_files $uri $uri/ $dir1/index.php?$args;
          }
      
          location ~ \.php$ {
                 include fastcgi_params;
                  fastcgi_pass unix:/var/run/php5-fpm.sock;
          }
      
          }
  3. wp_query

    I have 2 wordpress installs on one domain, main.com, and main.com/multisite.
    I got multisite working, but permalinks don’t work, I get 404 no matter what permalink structure I use (except default “p=1″). What could be the issue?

    • Rahul Bansal

      Can you share your config? (using http://pastebin.com/)

      • wp_query

        Here it is: http://pastebin.com/DcuB4r31. Thanks for looking into.

        • Rahul Bansal

          Can you also share error logs – /var/log/nginx/mysite.com.error.log ?

          Make sure you empty error log file and then produce erroneous behavior. After that send error log so I can see where things are going wrong.

        • Rahul Bansal

          Also install nginx-helper plugin – http://wordpress.org/plugins/nginx-helper/
          on your multisite setup.

          Activate it network-wide.

          Then go to a site, change permalink and check if permalink works after that.

          • wp_query

            I already had installed nginx helper, but I reinstalled it again, network activated, changed permalink structure to default, then to another, and I’m getting 404.
            Error log /var/log/nginx/mysite.com.error.log is empty, even after 404 error.

            Forgot to mention, once I click on permalink on multisite, Im redirected to 404 of main.com site, not multisite. But url stays like this: main.com/demo/multisite/hello-world/ (multisite is kept in url).

  4. wp_query

    Any ideas, something else I should try?


Comments are closed on this article.
Please use our free support forum for support and discussions.