Configure Host headers in Apache

Host headers allow you to map hostnames to web sites.  You can have multiple host headers pointing to the same IP, using the same port (generally standard port 80).

A common scenario when I use host headers is for SharePoint. In SharePoint, each web application is a web site in IIS.  I might have 3 web applications (central administration, intranet and mysites).  I can have all three web sites using port 80, with different host headers:

central.mydomain.com
intranet.mydomain.com
mysites.mydomain.com

The process is the same for all web servers.  For example in Apache, I have the following host headers setup on my network:

wordpress.devuxmachine.local
phpmy.devuxmachine.local
mail.devuxmachine.local

Today I am going to describe how to do it in Apache.

First, I am going to setup some local host headers on the server, so we can use for testing.  Open the /etc/hosts file for editing.  Add the following lines:

127.0.0.1 wordpress.devuxmachine.local
127.0.0.1 phpmy.devuxmachine.local

Now you should be able to ping the hostnames above:

$ ping wordpress.devuxmachine.local
PING wordpress.devuxmachine.local (127.0.0.1) 56(84) bytes of data.
64 bytes from wordpress.devuxmachine.local (127.0.0.1): icmp_req=1 ttl=64 time=0.017 ms

Now we want to add a virtual host to Apache.  Lets say we have the following setup:

<VirtualHost *:80>
 DocumentRoot /var/www/wordpress
</VirtualHost>

The above should display your wordpress site using http://yourserver:80. To tell Apache to listen for the host header instead, add the following line:

<VirtualHost *:80>
 ServerName wordpress.devuxmachine.local
 DocumentRoot /var/www/wordpress
</VirtualHost>

Now you should be able to access your wordpress site using http://wordpress.devuxmachine.local.

If you have DNS configured, you can add this as an A record, which will allow all users to use this host header.  Otherwise (for example for use in a development environment), you can always edit your /etc/hosts file (in Linux), or your c:/windows/system32/drivers/etc/hosts file in Windows.