How to implement WordPress in a subdirectory, hosted on a different server?
The subdomain is certainly the easiest option since you are wanting to host the site on another server. The reason for this is that a hostname can only ever resolve to a single location. If you are worried about SEO from the changed URLs, you could perhaps consider a redirect to the subdomain instead.
However, since you are specifically looking to avoid the subdomain, the rest of this answer will focus on how to do that.
If you want /blog/ to be served from a different server, then you would need to create a reverse proxy. Depending on your level of access to the current web server, the best option would be to create a reverse proxy. The following instructions may require some skill with server changes to implement and troubleshoot.
Using Apache
Assuming that you are using Apache, you could add in a rule like this:
ProxyPass /blog/ http://example.com/blog
ProxyPassReverse /blog/ http://example.com/blog
You could place this in your VirtualHost entry, or somewhere in your httpd.conf. This assumes that you have mod_proxy
installed, and it will require an Apache restart.
If you are using cPanel, which is popular, you can look here for locations to place a file with a name ending in .conf
: https://documentation.cpanel.net/display/EA/Modify+Virtualhost+Containers+With+Include+Files
If you are using cPanel, mod_proxy
should be included, so you shouldn't have to worry with that, but you will need to /scripts/rebuildhttpdconf
and then restart Apache.
This would allow you to make a connection to another location to grab the actual blog pages to serve through your current server.
The problem with WordPress, like many CMS, is that it is very picky about the URL you use to access it. That means that if you were to connect to a subdomain, if the siteurl didn't match, WordPress would serve a 404. Also, WordPress will often output redirects with the siteurl in it. Therefore, you would likely need to make the server think you are connecting to the same URL, even though you are connecting to a remote server. The tricky part is is that you would also need root access to be able to modify your server's hosts
file. On a Linux server, you would find that at /etc/hosts/
, and you could add a line like this:
123.123.123.123 example.com
Where 123.123.123.123
would be the IP of the server where you would be hosting the blog. Of course, this will only work if nothing else on that server is expecting to connect to example.com.
Using Nginx
If you're using Nginx, which is a bit less common, you could do this a little easier:
upstream blogbackend {
server 123.123.123.123:80;
}
location /blog {
proxy_pass http://blogbackend;
}
Because Nginx allows you to specify an IP for the backend, you shouldn't have to play with the hosts
file.
Siteurl
In both cases, the remote server with the blog on it should be configured to serve content for http://example.com/blog
, and that wold be the option_value
for both siteurl
and home
in the $prefixoptions
table. If that's the original URL, then you shouldn't have to change it. If you do have to make changes, be prepared to check any hard-coded URLs, such as uploaded images, etc.
Conclusion
This solution is a bit messy, and there's a lot that can go wrong. However, it's still probably cleaner than the next alternative, where you have /blog/ serve content through a PHP proxy, perhaps using curl. This is the reason why the standard approach would be to simply use a subdomain.