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 unix:/var/run/php5-fpm.sock;
        }

        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 Comments

  1. 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. Author

    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.

    • 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. 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?

  4. Any ideas, something else I should try?


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

People Who Like Thisx

Loading...