I have this in my sites-available apache2 virtual host file:

 <VirtualHost *:80>

   ServerName  domain.com
   ServerAlias www.domain.com


Why does apache send requests from www.domain.com to the correct path, but domain.com gets sent to the default virtual host?

Update: The reason domain.com gets sent to the default virtual host is because the hostname of the machine automatically gets sent to default.


(Obvious question -- is the DNS for www and non-www the same?)

For debugging purposes the -S flag is useful to check the config as the apache service itself sees it.

Debian based world:

apache2 -S

Redhat based world:

httpd -S

and another example:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server localhost (/etc/apache2/sites-enabled/000-default:2)
         port 80 namevhost mydomain.com (/etc/apache2/sites-enabled/domain1:16)
         port 80 namevhost myotherdomain.com (/etc/apache2/sites-enabled/domain2:1

I'd check to see if you have the proper DNS records for domain.com and www.domain.com - where there is 1 main A record, and then a CNAME for the other.

You can do lookups with the tools at network-tools.com


Make sure you have DNS entries set for www to point to domain.com.


Is there a servername directive outside of the < virtualhost > section?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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