Apache – Virtual Host Based on CNAME

apache-2.2domain-name-systemlinuxvirtualhost

Can I make a vhost on apache to respond to a cname request? For example:

A-record of example.com is 1.2.3.4
foo.example.com is a CNAME for example.com

in http.conf:

<VirtualHost *:80>
 ServerName example.com
 DocumentRoot /var/www
</VirtualHost>

<VirtualHost foo.example.com>
 ServerName foo.example.com
 DocumentRoot /foo/www
</VirtualHost>

apache starts fine and digests my conf, but when I visit foo.example.com I get the content in /var/www, not the one in /foo/www. I think I'm on the wrong track here.

Best Answer

The following snippet should work (without any warning):

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www
</VirtualHost>

<VirtualHost *:80>
 ServerName foo.example.com
 DocumentRoot /foo/wwww
</VirtualHost>

Make sure you read the documentation on Name-based Virtual Host Support to understand why it didn't work in the first place.

Related Topic