How to move a WordPress site from root folder to a sub directory while using nginx

Lately I had to move my WordPress blog from http://example.com to http://example.com/blog since I had to host a different site at http://example.com. My WordPress blog was running on nginx and Ubuntu 16.04 LTS.

If you are using nginx to run WordPress, you can perform the following steps to move your WordPress site to its own sub-directory

Update the URL in WordPress

  1. Login to your WordPress blog at http://example.com/wp-admin
  2. Goto Settings->General and update WordPress Address (URL) and Site Address (URL) from http://example.com to http://example.com/blog.

As soon as you click “Save Changes”, the URLs will update and the WordPress site will stop working. We will fix this in the next step.

Move WordPress files from the root folder to sub-directory

  1. Login to your root directory through FTP. Here, create a directory called blog.
  2. Move all the WordPress files from their current location to the newly created folder blog/

At this stage, you will find that you can access your WordPress site at the new address. There is still one caveat: custom WordPress permalinks don’t work yet.

Reconfigure nginx to fix permalinks

  1. Connect via SSH and open the file /etc/nginx/sites-enabled/example.com for editing.
  2. You will find the ‘location block’ – a block of text that appears like this:
    location / {
    try_files $uri $uri/ /index.php?$args;
    }

    Just below this block, add the following lines:
    location /blog/ {
    try_files $uri $uri/ /blog/index.php?$args;
    }
  3. Save and exit.
  4. Restart nginx sudo systemctl reload nginx.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.