Apache Name Based VirtualHosts and DNS "www" issue

1

I'm not sure how to solve my problem. It is that my Apache2 is configured to serve 3 different VirtualHosts. That depends on the Domains.

The config is:

NameVirtualHost example.eu:80
<VirtualHost example.eu:80>
        DocumentRoot /var/www2
        ServerName www.example.eu
        # Other directives here
</VirtualHost>

NameVirtualHost example.de:80
<VirtualHost example.de:80>
    DocumentRoot /var/www3/drupal
    ServerName www.example.de
</VirtualHost>

NameVirtualHost test.de:80
<VirtualHost test:80>
    DocumentRoot /var/www1/drupal
    ServerName test.de
</VirtualHost>

If I go to www.example**.de** I arrive at example**.eu** (the first configured VirtualHost). Same thing with test.de. It seems the www. is misinterpreted by that config. What do I have to do to make Apache2 handle this correctly, so that with and without "www." I get where I want - each time. All DNS entrys go to the same Apache2 server IP. The server is supposed to handle the requests accordingly. With and without the "www" prefix.

Best, ww

www

Posted 2011-10-13T17:04:11.363

Reputation: 31

Answers

2

You misunderstood what means the hostname in <VirtualHost> directive. It refers to an IP address where the virtualhost "listens" (Apache can resolve the names to IP address). So, I recommend you a following configuration:

NameVirtualHost *:80
<VirtualHost *:80>
        DocumentRoot /var/www2
        ServerName example.eu
        ServerAlias www.example.eu
        # Other directives here
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www3/drupal
    ServerName example.de
    ServerAlias www.example.de
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www1/drupal
    ServerName test.de
    ServerAlias www.test.de
</VirtualHost>

You must use NameVirtualHost domain.name:80 or NameVirtualHost 1.2.3.4:80 if your virtual host listens different IP address than the default.

Gabor Garami

Posted 2011-10-13T17:04:11.363

Reputation: 299

default VirtualHost overlap on port 80, the first has precedence - that's what I get now. I don't think that is the solution. – www – 2011-10-13T17:21:57.263

Because you have a virtualhost with header like <VirtualHost _default_:80>. Try to disable it. – Gabor Garami – 2013-07-09T22:12:16.983