Main Content

Telling which ServerAlias your visitor used - useful during merging domains

Archive - Originally posted on "The Horse's Mouth" - 2012-01-04 15:51:21 - Graham Ellis

Someone tells you a website to visit - "go to wellhousemanor.co.uk" they say. And you'll naturally add "www." in front of it, calling up www.wellhousemanor.co.uk rather than wellhousemanor.co.uk ... or perhaps you won't.

Web servers are normally set up by default to respond to a single specific host name - either wellhousemanor.co.uk or www.wellhousemanor.co.uk in the example that I've used above. But then, if you
a) Configure your DNS records for both www.wellhousemanor.co.uk and wellhousemanor.co.uk to point to the same server and
b) add a ServerAlias as well as a ServerName in you virtual host definition:
  ServerName www.wellhousemanor.co.uk
  ServerAlias wellhousemanor.co.uk

you can set up a single web site to respond seamlessly to either name (with or without) the "www.". This process can be taken further so that the same host can respond in the same way to other domain names by adding further DNS records and further ServerAlias-es. I will question how wise it is to do a lot of this, as your content may end up being "diluted" across a lot of domains as far as search engines are concerned, and you may end up loosing visibility. Beware!

Ironically, there may be occasions where you have two host names pointing at the same virtual host, and you want to know which was the one used to access your site by your visitor.

• In a PHP program, you can use the $_SERVER[HTTP_HOST] variable (and there will be equivalents in other languages / Frameworks

• Within your Apache configuration, you can refer to %{HTTP_HOST} - for example, if www.wellhousemanor.co.uk and wellhousemanor.co.uk shared the same virtual host, the following lines in your .htaccess file would allow you to divert requests for anything in the /info directory to another site, provided that the original call was to wellhousemanor.co.uk rather than to www.wellhousemanor.co.uk

  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^wellhousemanor.co.uk$
  RewriteRule ^/info http://www.wellho.net/ [L]


This technique - not well documented elsewhere as far as I can see - may be a great help if you're in the process of merging domains, but still have one or two rather obscure sections of your site / the domains which are still to be integrated under a single name.