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:

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


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: wordpress.devuxmachine.local phpmy.devuxmachine.local

Now you should be able to ping the hostnames above:

$ ping wordpress.devuxmachine.local
PING wordpress.devuxmachine.local ( 56(84) bytes of data.
64 bytes from wordpress.devuxmachine.local ( 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

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

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.



2 thoughts on “Configure Host headers in Apache

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s