Search code examples
apache.htaccessurl-rewritingdnssubdomain

Hosting subfolders on different domains


I would like to host my content on two different servers. There is the main website content domain.com/main-stuff, and there is the campaign pages content domain.com/landing-pages/other-stuff.

Is there a way for me to host these two things on different servers without using a subdomain?

I want to still show the users (and Google!) domain.com/landing-pages/other-stuff but that stuff would be hosted at lp.domain.com/other-stuff

Any guidance would be much appreciated.


Solution

  • This certainly is all possible from a technical point of view. You'd have to implement a "reverse proxy" (comes out of the box) for this. In some simple cases even a normal "forward proxy" might be enough, that depends on your content, though. Anyway you'd need a separate DNS entry but it does not matter whether you are using a subdomain or a completely different domain on those two servers.

    Other solutions are possible but less seamless. You could for example sync the content of both servers by syncing file system content and probably database content. But that is a big topic and comes with lots of ToDo's and hassles.

    Two questions arise if you want explore the usage of a proxy :

    1. Is your platform suitable for that. This is probably not the case if you are using a cheap hosting provider or one of those "app providers" limited to specific technology stacks. It should be no issue if you operate those servers yourself however.

    2. Is it a good idea considering the drawbacks of the solutions. Those requests traveling through your proxy will have a much worse performance than those directly hitting the actual content holding server. Since each and every request and each and every response has to travel through both servers with some latency and all the network distance between the two, since those requests each have to be forwarded and rewritten one by one (read: in a sequential manner).

    In general you need a proxy module loaded and activated inside your frontend server (that one serving only the landing pages). In case of the apache http server this means the two modules "mod_proxy" and "mod_proxy_http". Given that you can then proxy the requests to that server to be responded to by the backend server (your main content hosting server):

    Two alternatives:

    First you can use the proxy module directly:

    ProxyRequests off # "off" is correct, not "on"
    ProxyPass /other-stuff https://example.com /landing-pages/other-stuff
    ProxyPassReverse /other-stuff https://example.com /landing-pages/other-stuff
    

    Or you can use the same module embedded inside the rewriting engine:

    RewriteEngine on
    RewriteRule ^/?other-stuff/ https://example.com /landing-pages/other-stuff [P]
    

    Comes out the same, the second approach gives you more abilities when it comes to handling exceptions.

    In case all links inside those landing pages point to the main server anyway (so you host only the landing pages themselves on the frontend server) you might get away with a simple forward proxy. In that case you could leave away the ProxyPassReverse directive in the first approach and speed the response up a bit. But you then need to take care to add valid CORS headers to the response so that fetching things like style sheets and script files works. That'd make things complex again, so it probably makes more sense to go for a reverse proxy as shown.

    Both approaches are best implemented inside your http server's central host configuration. If you do not have access to that you can also use a distributed configuration file (typically called ".htaccess"), but that comes with a few disadvantages. You also need to make sure that that wil is located in the http host's DocumentRoot folder, that it is readable by the http server process and that you enabled it's interpretation by means of the AllowOverride directive in the central configuration. Take the official apache documentation as a reference for those points. It is, as typical for OpenSource software, of excellent quality and comes with great examples on such topics.

    As hinted above it is irrelevant what domain (or "subdomain"), actually host name you are using on the frontend server, as long as you have a valid DNS A or DNS CNAME record that points requests to that host name to that server.