Apache virtual host based on CNAME

19,914

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.

Share:
19,914

Related videos on Youtube

accidental admin
Author by

accidental admin

Updated on September 17, 2022

Comments

  • accidental admin
    accidental admin over 1 year

    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.

  • Dave Hassall
    Dave Hassall about 14 years
    For those visitors whose eye's may be glazed from reading hundreds of forum posts and irrelevant results from Google searches: The key part is including the "NameVirtualHost" directive that @joschi includes in his snippet above. Name-based virtual hosts don't work without a NameVirtualHost directive.
  • Gert van den Berg
    Gert van den Berg about 7 years
    If <VirtualHost secondary.ip:80> is present as well, you need a NameVirtualHost secondary.ip:80 as well. The NameVirtualHost *:80 does not cover it... (And <VirtualHost *:80> does not seem to work on the secondary IP for additional VirtualHosts)