Migrating WordPress from Subdomain to be Subdirectory under Laravel and Nginx
My company originally has a blog using WordPress served under a subdomain, let’s call it blog.foo.com. One day, there was a requirement to move the blog to be served under a subdirectory called foo.com/blog for the SEO purpose. I’ve googling around and found many articles written about doing this under Apache. However, not that much article talking about doing this under Nginx. So, for this article, I will write about how to migrate WordPress from a subdomain to be a subdirectory under Laravel and Nginx. It is very simple and not complicated at all, and I hope that this can reduce the time doing the migration more or less.
Prerequisites
- You website is served under Nginx.
- Virtual hosts are properly configured and working.
- You already have a blog serving under Nginx on a subdomain e.g. blog.foo.com.
- You already have another Laravel website serving under Nginx on a root domain e.g. www.foo.com
- You have access to WordPress db.
Start migrating
- Go to your main web directory. If you don’t know the path, you can check the
root directive in your site configuration file in
/etc/nginx/sites-available . Most of the time, the filename should be the same or similar with your domain name e.g.
foo.com.conf . The path should look like
1root "/home/vagrant/foo.com/public"; - Go to the path you’ve found above and create a symlink to the WordPress directory. Again, if you don’t know the path, you can repeat the step above to get the path. To create a symlink, you can use this command:
1ln -s <blog directory path> blog - Now, when you run
ls -l command, you should see the symlink pointing to your WordPress directory.
1lrwxrwxrwx 1 vagrant vagrant 22 Apr 6 15:50 blog -> /home/vagrant/blog.foo.com/ - Edit your main directory Nginx configuration file, in this case, I use
nano for editing.
1sudo nano /etc/nginx/sites-available/foo.com.conf - Put a new
location block as follows:
12345location /blog {alias <WordPress directory path>;index index.html index.htm index.php;try_files $uri $uri/ /blog/index.php?$args;}
You need to put your symlink path you just created in the alias directive, and it should look like:
1alias /home/vagrant/foo.com/public/blog; - Restart Nginx
1sudo servine nginx restart
Re-configure WordPress
After finish setting the Nginx configuration, now you need to change the “Site Address (URL)” and “WordPress Address (URL)”. There are several ways doing this that you could find from here. For me, I chose the update it through SQL. I used the below queries I found from wbeaches.com to update.
1 2 3 4 5 6 7 |
UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl'); UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl'); UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl'); |
Redirect the traffic
Still, there might be some old subdomain links that we already published on the Internet; for instance, Facebook. Therefore, to prevent the error if someone accesses the old links, we need to redirect all the traffic from the old subdomain to the new one by modifying the location block in your main website Nginx configuration file.
1 2 3 |
location / { rewrite ^/(.*)$ http://www.foo.com/blog/$1 permanent; } |
Make sure, you replace www.foo.com with your domain and have only rewrite in the block, and also remove all location blocks that have fastcgi related stuff from the file too.
Polylang
For some reasons, after I’ve done everything above, I still couldn’t access the blog with a new url, and after I change the URL modifications option of the Polylang from “The language is set from the directory name in pretty permalinks” to “The language is set from content”, everything is back to work again. For people who not facing this problem or not using Polylang, you can just skip this.