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
- Login to your WordPress blog at
http://example.com/wp-admin
- Goto
Settings->General
and update WordPress Address (URL) and Site Address (URL) fromhttp://example.com
tohttp://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
- Login to your root directory through FTP. Here, create a directory called
blog
. - 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
- Connect via SSH and open the file
/etc/nginx/sites-enabled/example.com
for editing. - 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;
} - Save and exit.
- Restart nginx
sudo systemctl reload nginx
.