How to Configure WordPress NGINX Rewrite Rules for a Subdirectory Installation

    Sumit Pore
Listen to this article

install-wordpress-ubuntuUndoubtedly, WordPress is one of the most simplest software to install. WordPress even boasts of its famous 5 mins installation. All you have to do is add a few PHP files to the your website’s root directory and you’re done! Almost every aspect of WordPress works fine on any web server you choose; may it be Apache or Nginx or any other server.

The only thing that could be a concern are- the rewrite rules.


What are rewrite rules?

In simple terms, rewrite rules allow you to have pretty permalinks. Essentially they are rules added, similar to formulas, which resolve the URL structure and redirect to the associated page. So, say your rewrite rules are in place, and you have set up your Permalinks settings to the ‘Post name’. A post title ‘Hello World’ post with the slug ‘hello-world’, will be accessible at http://<your-domain-name>/hello-world.

Now, let’s consider a scenario wherein you have a ready website, and you want to now set up another WordPress website ‘demo’ in a subdirectory, e.g., What you would have to do here, is you would have to go to the root directory of and add a new folder there ‘demo’. You would then have to install WordPress in that directory.

If you are on Apache, Apache will take care of writing appropriate rewrite rules in your .htaccess file so that all links of work well. You do not need to worry about anything with Apache.

But if you are on Nginx, you will find that post, pages and archive links of throwing a 404 error! It’s pretty frustrating! 🙁

But that’s what I’m writing this article for! To help you solve this issue. 😀 All we would need to do in such a case, would be to write some rules in the Nginx configuration file.

Let’s take a look.


Not a developer? Let our WordPress experts do the grunt work for you!

Contact us



Configuring WordPress NGINX Rewrite Rules

Since your main domain is, we need to edit the file /etc/nginx/sites-available/ or /etc/nginx/sites-available/

Check which of these files exist on your server and open that file.

Let’s assume that configuration file associated with the domain is So fire your terminal and login to SSH. Once you are logged in, fire the below command:

sudo nano /etc/nginx/sites-available/

In this file, search for a line which defines index like index index.php index.html index.htm; You might only have index index.php;

Below that line, we will need to add below code:

location  /demo {
     root /var/www/; 
     index index.php index.html index.htm;
     rewrite /wp-admin$ $scheme://$host$uri/index.php?q=$1 permanent;
     try_files $uri $uri/ @demo;

location @demo {
        rewrite ^/demo(.*) /demo/index.php?q=$1;


What does the above code do?

location  /demo {: What we’re doing here is, looking for requests made for the demo website. Only if the request is for the /demo website, e.g, or or, then we have to execute the loop.

root /var/www/; : We have to then indicate the location of the files for the demo website. (If your path is different than the root directory of the site, then enter the appropriate path here). Make sure you specify the absolute path.

index index.php index.html index.htm; : Next, we need to specify the default files to look for inside the directories. This means when a request is made for, it will pick index.php for execution. If index.php is not present, it will pick up index.html. If index.html is not present either, it will pick up index.htm

rewrite /wp-admin$ $scheme; ://$host$uri/index.php?q=$1 permanent: This step indicates that if a request is made for wp-admin, then the request should be redirected to wp-admin/index.php

try_files $uri $uri/ @demo; : This rule checks if any file or folder made in the request exists and if it does not, then the fall back path is set to @demo. For e.g., lets say a request is made for the ‘hello-world’ post. $url will check if any file named ‘hello-world’ exists on the file system. If that does not exist, it will move to $url/ which checks if a folder named ‘hello-world’ exists or not. If that too does not exist, then it forwards the request to @demo.

location @demo : As explained above, when a request made is unable to get any file or folder resembling the path requested, the request is forwarded to @demo. This block will define what should be done when requests are forwarded to @demo

rewrite ^/demo(.*) /demo/index.php?q=$1; : This rule defines that if any request comes to demo, it should be passed to index.php under the ‘demo’ folder along with any arguments. So, when we request for post, it will be internally redirected to index.php as WordPress then interprets the request and shows the content of the ‘hello world’ post. All front-end related requests are handled by this line.

After adding the above code in the configuration file, you can save the configuration file by pressing Ctrl + O followed by Ctrl + X to close the editor.

Now reload the Nginx configuration by firing the below command:

sudo service nginx reload

And, you’ve done it! Your demo site should work fine now. 🙂

And guys remember, if you experience any problems with setting up Nginx, PHP-FPM, or MySQL, you can always ask me your questions me, in below comments section below. Or if you need us to set up WordPress with Nginx for you, you can always contact us.

Contact Us

That’s it for the day. Adios 🙂

You may also like to read: How to Create Custom WordPress Rewrite Rules for Pretty Permalinks

Sumit Pore

Sumit Pore

One Response

  1. I need Help i have created 2 directories in my website one is and another is but after the install of wordpress in knowledge folder it gives me error 4o4 and working fine Find below virtual.conf file of nginx:
    location / {
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
    error_page 404 = @drupal;

    # wordpress Permalink to Work added below lines ####
    index index.php;
    if (!-e $request_filename)
    rewrite ^(.+)$ /blog/index.php?q=$1 last;

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads