Topics

On this page

Last updated on Dec 18, 2020

WordPress In Subdirectories Itself

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

wordpress-nginx

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


Comments

  1. himanshu Avatar
    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. 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.

    1. himanshu Avatar
      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;
          }
      
          }
      1. You need to tweak config. I pasted our config for reference.

        And please use support forum for https://rtcamp.com/support such queries.

  3. wp_query Avatar
    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?

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

      1. wp_query Avatar
        wp_query

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

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

        2. 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.

          1. wp_query Avatar
            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 Avatar
    wp_query

    Any ideas, something else I should try?

    1. In location ~ \.php$ { block, replace following line:

      try_files $uri /multisite/index.php;

      with

      try_files $uri =404;

      Then turn on nginx debug log for error_log and send it over in another pastebin.
      For debug instructions – http://rtcamp.com/tutorials/nginx/debugging/

      1. wp_query Avatar
        wp_query

        Right now I’m getting errors when Im starting nginx, and when I check nginx status it says not running but domain is loading…
        Here’s error log: http://pastebin.com/PkQ3ijSi
        And here’s updated nginx.conf: http://pastebin.com/UByz547E

        1. You forgot semicolon in try_files $uri =404;

          For “(98: Address already in use)” error, follow – http://rtcamp.com/tutorials/nginx/troubleshooting/emerg-bind-failed-98-address-already-in-use/

          And please use support forum for support – http://rtcamp.com/groups/wordpress-nginx/forum/