Configuring WordPress Nginx Rewrite Rules: How it work?

Nginx Rewrite Rules
Have you recently moved from WordPress Apache to Nginx server? then these Nginx rewrite rules will be useful for your WordPress blog.

No doubt, WordPress is one of the most simple to use development platform. In fact, installing WordPress takes only a few minutes to complete. You just need to upload a few PHP files in the root directory of your site and it will be ready to use. Setting up a WordPress instance on the Nginx web server, or Apache or any other web server.

However, one major concern you might have to deal with is: the rewrite rules.

In this post, we’ll focus on the process of configuring WordPress NGINX Rewrite Rules. But let us first understand what do we mean by rewrite rules.

An Overview of Rewrite Rules

If you’ve been working with WordPress for quite a while, then you will certainly be aware of the significance of having pretty permalinks also called as clean URLs. By pretty permalinks, we mean creating SEO-friendly permalink structures.

Rewrite rules basically help WordPress site owners to have pretty permalinks. To be more specific, the rules help in correcting the URL structure and redirect them to the respective page. Now, suppose you’ve applied, rewrite rules and setup “Post name” like ‘My first blog’ as your Permalinks settings. In that case your post titled as ‘My first blog’ post and its slug ‘my-first-blog’, can be accessed by visiting: http://<yourdomain-name>/my-first-blog.

Let us now consider a scenario, in which you’re running a WordPress site with the name, and you would like to set up another site ‘test1’ in the subdirectory, i.e. To do so, you’ll need to go to your current site’s ( root directory and add a new folder named ‘test1’ within that directory. Finally, you need to install the WordPress software in that directory.

In case you run your site on Apache, then you won’t have to worry much about the rewrite rules as the Apache server handles the process of writing suitable rewrite rules within your .htaccess file. This ensures that all of the links on your site will work well.

However, you’ll encounter an issue when your site is running on Nginx web server: the pages/posts and archive links on will throw a ‘404 error’. This can be a lot frustrating for you. You can resolve such an issue, by writing a few rules within the Nginx configuration file.

Related:  How to change WordPress site title and tagline

Understanding How to Configure WordPress NGINX Rewrite Rules

Since the domain name of your current site is, we’ll have to make a few tweaks to the file: /etc/nginx/sites-available/ or /etc/nginx/sites-available/

Next, check which one of the above-mentioned files is present on your server and open it. Suppose that the configuration file belonging to your domain is In that case, open up your terminal application and login using the “SSH” command. After getting logged in, run the following command:

And then, in the file, look out for a line defining the index in the form of “index.php index.html index.htm”. Chances are that you might only find the line index index.php. If that’s the case, then below that line add the following code:

Let us understand what does the above code do, line by line:

1. location /test1 {

Here we’re searching for the requests that have been made for the demo site. The loop will be executed only if a request is made for the demo site, i.e. or

2. root /var/www/;

After looking for the requests for the demo website, we’re determining the location of the files for that site.

Note: Do make sure to enter the appropriate path here that matches the root directory of your existing site. If not, then specify your path here.

3. index index.php index.html index.htm;

This line of code is specifying all the default files to explore the directories. Meaning that whenever a request is made to access, the index.php file will be selected for execution. In the absence of the index.php file, the index.html file will get executed or else the index.htm will be executed.

Related:  Installed WordPress, but what next to configure and setup ?

4. rewrite /wp-admin$ $scheme://$host$uri/index.php?q=$1 permanent;

This step determines that in case a request is made for accessing wp-admin, then it needs to be redirected to the path wp-admin/index.php.

5. try_files $uri $uri/ @test1;

This line of code evaluates whether any particular file or folder that has been requested exists or not. If not, then the path of the fallback will be set to @test1.

6. location @test1

As we’ve discussed above, in the case a request fails to get access to any file or folder – that matches the requested path – the request will be forwarded to @test1. The “location @test1” block will help define what you need to do when the request is redirected to @test1.

7. rewrite ^/test1(.*) /test1/index.php?q=$1;

Lastly, this step specifies that if a request is forwarded to the demo site (i.e. test1), then it ought to passed to index.php first that can be found under the ‘test1’ folder together with arguments. Thus, while a request is made for post, then the request will be internally forwarded to index.php as And then, WordPress will interpret that request and will display the entire content of the post ‘My first blog’.

Once you’ve added the above code snippet in the configuration file, save the file by clicking on “Ctrl + O” first, and then press “Ctrl + X” to exit from the editor.

Finally, reload your Nginx configuration file by executing the following command:

sudo service nginx reload

That’s it! Your demo website (i.e. test1) will start working in the correct manner.


Hope that reading this post will help you resolve the issue associated with Nginx rewrite rules, and how you can configure them correctly when installing a sub-directory.

Author Bio:

Sophia Phillips has been working as a professional in WordPress theme customization company and loves sharing information about leveraging multiple benefits of WordPress CMS in the best possible manner. Currently, she has an impressive count of WordPress – related articles under her name.

You may also like...

Leave a Reply

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