another man's ramblings on code and tech

Hosting Multiple Domains Off One Apache Server With No Virtual Hosts


Note: I don't use this scheme for my webhosting anymore, but I'll leave this here as a fun facts article

It's been awhile, blog! Things got hectic and you got pushed to the side for a little while. I've graduated and gotten a job since my last post!

Anyways, when it comes to hosting Apache servers, the main way you would normally host multiple domains on one machine is with Virtual Hosts. I had no success attempting this route, and I think it was network misconfiguration of the router on my part. But, I realized that simple Apache Virtual Hosts wouldn't solve a series of complicated problems behind my home router, even with some fancy forwarding and routing. So, I had three domains to connect to one server: my portfolio site at justinflowers.ca, my personal blog at justinflowers.me, and my tech blog jflow.io. Just as a note: the reason I never set up these hosts before was because I had them all hosted over justinflowers.ca, with me recently picking up the last two domains. So, what simple strategies did I have at my disposal? There were two main ones:

1. URL redirection over DNS with masking

URL redirection with masked routes would have worked well in my case if I wasn't running blogs off of the site. URL redirection involves simply forwarding the user from one domain to another on connection. This way you can have multiple domains point to one server, like justinflowers.ca and justinflowers.tech. With masking you hide the URL you're redirecting to and instead only show the one the user originally connected to. For example, if you connected to justinflowers.tech you would be redirected to justinflowers.ca. With masking you would only see justinflowers.tech, no matter where you went. You see, masking will hide the full URL to the user no matter where they are directed. I didn't like this because it provided no unique way of accessing my pages, in particular my blog posts. Therefore, this choice was scratched off. For single serving or page sites, though, masking would be a great choice.

2. URL redirection through PHP with dynamic DNS

The choice I ended up going with was URL redirection through PHP and multiple dynamic DNS entries pointing to one server. I started by creating or updating each site I wanted to point to with domains. In this case, I updated this blog's theme and installed a new personal blog on the main Apache server. I then pointed each domain to the server's public facing IP with dynamic DNS entries, such that no matter what you connected to the same index.php file. In that _index.php _file I simply check the URL that made this connection and forward traffic to the right directory based on which domain it contains. It should be noted that this required a new entry in the server's dynamic DNS for each unique domain to point to it. Here's a snippet of code from that _index.php _file:

<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

// Forward to tech blog if URL contains 'jflow.io'
if (strpos($url,'jflow.io') !== false){
 header( 'Location: http://blog.jflow.io/web/wordpress/' ) ;
 return;
}

// Forward to portfolio site if URL contains 'justinflowers.ca'
if (strpos($url,'justinflowers.ca') !== false){
 header( 'Location: http://main.justinflowers.ca/web/index.php' ) ;
 return;
}

//... continue for all possible unique domains
?>

This allows me to point many domains to one server simply while maintaining different landing pages for them. It's a quick and dirty slight of hand; there are oddly specific URLs that will end you up in the wrong location. Given that, however, if the user puts in any URL without a directory after the TLD, like _jflow.io, blog.jflow.io, imamonstertruck.jflow.io, _they will be routed to the correct location.

Anyways, I've had a fun weekend migrating my server from an old laptop to an old school lab computer I got for $10, so that's what's caused all this activity. I already have a few ideas for interesting posts moving forward based on what I've learned at my new job, so look forward for more soon!

Date: Jun 27 2016

PREVIOUS NEXT