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

  1. 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
  2. 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:
  3. Now, when you run ls -l  command, you should see the symlink pointing to your WordPress directory.
  4. Edit your main directory Nginx configuration file, in this case, I use nano  for editing.
  5. Put a new location  block as follows:

    You need to put your symlink path you just created in the alias directive, and it should look like:
  6. Restart Nginx

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.

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.

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.

prapat
  • George Alvis

    Thank you for the simple tutorial,
    I want to access the wordpress directory on the root domain
    How can i achieve that?