8

I am working with a client who already has a custom e-commerce website set up at their own domain e.g. example.com

We want to host their WordPress blog at a subdirectory of their domain e.g. example.com/blog Due to security issues we cannot host WordPress on the same server as the rest of their site.

We have set up a separate server which will host the WordPress blog, and given it a temporary subdomain URL.

How do we make it so that example.com/blog will serve the Wordpress site from our server? We don't want a redirect, we want it to appear as if all of the blog is hosted within the '/blog' subdirectory. What URLs do we put in the WordPress database? How should they be redirecting their subdirectory?

I know people recommend using a subdomain (this is what we currently have), but the client has specifically requested using a subdirectory for better SEO performance.

2

2 Answers 2

6

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.

7
  • ProxyPass and ProxyPassReverse (if I remember correctly) requires that a module be included which is not always included by default on all installations. It also requires restarting Apache. I seem to remember having to make configuration changes before ProxyPass and ProxyPassReverse would work on my test Apache 2.4 install. Worth mentioning in your answer. Cheers!!
    – closetnoc
    Jul 26, 2017 at 18:24
  • @closetnoc Thanks. I was thinking that I was already getting a bit too deep into system administration. I added a bit of a disclaimer, too.
    – DKing
    Jul 26, 2017 at 20:01
  • Thanks for this detailed response @DKing - My client has a developer who is dealing with all the server stuff, so I think they have done this as a reverse proxy using Nginx. I updated the URLs in our WP database to use the URL of the new subdirectory. Everything appears to be working fine now, however there are errors within the wp-admin. Specifically a "502 Bad Gateway nginx" error when trying to create a wordpress post. Also an access denied message when trying to save wp theme options. Could these be caused by the proxy you think, or a wp misconfiguration? Thanks again!
    – J. Purcell
    Jul 28, 2017 at 0:35
  • @J.Purcell The 502 may be related to nginx, or it may not, but saving theme options would likely be a WordPress issue, such as perhaps a permissions issue. The proxy is simply passing the information on, so once you are modifying the theme, that should be being done at the upstream server, so I would investigate it there, first. I would handle that before looking at the 502 issue.
    – DKing
    Jul 28, 2017 at 14:06
  • 1
    @Baumr Sure. I have posted a response.
    – DKing
    Jul 25, 2018 at 20:16
0

Your server has an IP address, so you can just grab that and then create a subdomain wherever the DNS for the domain is hosted. So say you want to create blog.domain.com, you can point that to your Servers IP address.

1
  • Thanks for the reply, but the question was how to point a sub-directory to another server, not a sub-domain. However my issue was resolved in my collaborations with other developers, using a reverse proxy technique. Thanks!
    – J. Purcell
    Nov 1, 2017 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.