0

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>

3 Answers 3

3

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

1
  • 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. Jul 6, 2009 at 7:37
1

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.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .