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

I've tried to use NamedVirtualHosts to setup a single server to handle two websites. This is the first time I've attempted this, and I'm getting very unusual behavior. Specifically, requests to www.example2.com point o /home/stefan/example2, but http://example.com points to /home/stefan/example1.

Does anyone have any idea what's going on here?

/etc/apache2/apache2.conf

...

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot /home/stefan/example1
   ServerName www.example1.com
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot /home/stefan/example2
   ServerName www.example2.com
</VirtualHost>
share|improve this question

3 Answers 3

up vote 3 down vote accepted

I am guessing that what you really meant to say was that www.example2.com goes to the right place, but example2.com goes to the www.example1.com site.

You need to use the "ServerAlias" directive and and insert the domain without the "www" in there.

Something like following in the appropriate section:

ServerAlias example1.com

ServerAlias example2.com

share|improve this answer
    
Exactly what I needed. Thanks! –  Stefan Kendall Jul 6 '09 at 3:17
2  
The explanation as to why you're seeing this problem is that www.example2.com and example2.com are considered different hosts by apache. You don't have a virtual host configuration for example2.com, so apache uses the default vhost, which is the first one configured, which in your case happens to be www.example1.com. –  David Pashley Jul 6 '09 at 7:37

What's the problem, sounds like it's working perfectly.

You have a vhost for www.example2.com going to the right place. Any hostnames that you don't have a vhost for (like example.com) will go to the first vhost.

share|improve this answer

From the Apache2 docs -

Due to the fact that www.example.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first VirtualHost.

So, since you don't have a vhost defined for example.com, Apache will use the default vhost. In your case, this is www.example1.com.

share|improve this answer

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.