Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

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|improve this answer
1  
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. –  anschauung Mar 19 '10 at 20:43
add comment

Did you try adding the port number on the foo.example.com vhost ?

ie:

<VirtualHost foo.example.com:80>
    ServerName foo.example.com
    DocumentRoot /foo/www
</VirtualHost>
share|improve this answer
add comment

I got it working like this:

NameVirtualHost _default_:80

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

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

Although this gives a warning at http start:

[warn] NameVirtualHost _default_:80 has no VirtualHosts
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.