Look before you leap, think before you adjust the Apache settings!

I’m sure not everyone will have the same problem – several different URLs pointing at one server, all served by one Apache server? Maybe you’ve bought a few similar domains? Maybe tezk.co.uk, tezk.uk and tezk.home? Though through slightly sloppy web page creation, some of the links will direct you to one of the other sites… Not a problem! Until you realise the cookies you’ve created on one domain wont necessarily work on another domain, resulting in logged in users being forced to log in several times! A good argument for never specifying the host in links, only the relative path.

Anyways, how to force people to use just one domain? Here’s where Apache’s redirect feature comes into play! Where as previously you might have had the following in your sites configuration file:

<VirtualHost *:80>
           ServerName www.tezk.co.uk
           ServerAlias  tezk.home
           <directory /var/www/html/tezk>
           </directory>
</VirtualHost>

So the server responds to a host request on any of those addresses, simply use the redirect feature!

<VirtualHost *:80>
           ServerName tezk.home
           Redirect "/" "http://www.tezk.co.uk/"
</VirtualHost>

<VirtualHost *:80>
           ServerName www.tezk.co.uk
           <directory /var/www/html/tezk>
           </directory>
</VirtualHost>

Simple! Now, any request to tezk.home will result in a 3xx code with a redirect! The browser simply requests the tezk.co.uk web page and the user is none the wiser, other than not having cookie related login issues!

Though there is one problem this setup does create… What happens when we request the website using the ip address? Or use a script or curl to pull data from the site? Due to the ordering here, if the site name isn’t specified, we match the first listed, and so we get redirected! Unless the script is configured to follow redirects (And how many are?) the script will fail and you’ll be left scratching your head!

So how to resolve it? Listing the redirects after the main page definition will set the “default” page to our main site, which does fix the script issue, but does mean anyone requesting another site from us (Using the “Host” http header) will be served up our main site. But then again maybe that isn’t such a problem? The other way would be to ensure the “host” http header is set correctly! This is what Apache looks at when checking the virtual hosts!

It seems a related server and an App in the Google play store had been broken since the migration due to them not specifying the “host” properly…

Leave a Reply

Your email address will not be published. Required fields are marked *