I'm trying to add a second virtual host to my apache configuration, but cannot seem to get the new virtual host to be used.

My httpd.conf just contains the following line:

ServerName radiofreebrighton.org.uk

I also have a ports.conf file, which contains the following:

NameVirtualHost *:80
Listen 80

<IfModule mod_ssl.c>
    Listen 443
</IfModule>

I have two files in sites-available which were symlinked to sites-enabled by a2ensite:

  • radiofreebrighton.org.uk
  • trafalgararches.co.uk

The contents of the first is:

<VirtualHost _default_:80>
    DocumentRoot /home/tom/www

    ServerAdmin tom@radiofreebrighton.org.uk
    ServerName radiofreebrighton.org.uk
    ServerAlias www.radiofreebrighton.org.uk

    <Directory /home/tom/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log
    LogLevel error
    CustomLog /var/log/apache2/access.log combined

    Alias /wiki /home/tom/www/mediawiki/index.php
</VirtualHost>

The contents of the latter is:

<VirtualHost *:80>
    DocumentRoot /home/tom/tata-www

    ServerAdmin admin@trafalgararches.co.uk
    ServerName trafalgararches.co.uk
    ServerAlias www.trafalgararches.co.uk

    <Directory /home/tom/tata-www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>

    logLevel error
    ErrorLog /var/log/apache2/error.log
</VirtualHost>

But any time I request a page from trafalgararches.co.uk, I am given a page from radiofreebrighton.org.uk. Why might this be happening? How can I fix it?


Edit:

Virtual host configuration as understood by apache:

tom@rfb:/usr/local$ apache2ctl -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server radiofreebrighton.org.uk (/etc/apache2/sites-enabled/radiofreebrighton.org.uk:1)
         port 80 namevhost radiofreebrighton.org.uk (/etc/apache2/sites-enabled/radiofreebrighton.org.uk:1)
         port 80 namevhost trafalgararches.co.uk (/etc/apache2/sites-enabled/trafalgararches.co.uk:1)
Syntax OK

(Gleaned via apache2ctl -S aka httpd -S.)

link|improve this question

1  
Not sure this is the cause, but you ought to remove the slashes from the ends of your ServerName and ServerAlias lines. Also, make sure you've restarted apache. – ErikA Jul 8 '11 at 12:53
I'm almost positive that's the cause. It means the hostname in the request will never match the ServerName for that virtual host. – larsks Jul 8 '11 at 12:57
@ErikA, @larsks - You guys got my hopes up! I removed the trailing slashes and restarted apache, but it didn't change anything. – Tom Wright Jul 8 '11 at 13:12
Do you have NameVirtualHost *:80 somewhere in your configuration? – larsks Jul 8 '11 at 13:15
1  
I am not sure if the sites mentioned above are production or development, but actually going to those URLs does give two different pages for me (correct pages by the looks of it). If the vhosts above are on a development server, ignore this comment. Otherwise, you might have fixed your problem somewhere along the way and still have a cached copy sitting around. – cyberx86 Jul 8 '11 at 17:30
show 7 more comments
feedback

2 Answers

Tom, please look here http://httpd.apache.org/docs/2.0/en/mod/core.html#namevirtualhost

Note

Note, that the "main server" and any default servers will never be served for a request to a NameVirtualHost IP address (unless for some reason you specify NameVirtualHost but then don't define any VirtualHosts for that address).

So it should be okay if you change the default to the ip-adress of your server.

link|improve this answer
I tried that, but it didn't change anything. I'd also like to avoid tying myself to a particular IP, so I'm going to change it back. Also, I should point out that the default vhost is the one that works. – Tom Wright Jul 8 '11 at 13:52
feedback

With your NameVirtualHost set to *:80 you'll need to make sure all your VirtualHost entries are exactly the same.

So changing the <VirtualHost _default_:80> to <VirtualHost *:80> should sort you out.

Also consider replacing the ServerAlias with another VirtualHost containing only a RedirectMatch 301. This will avoid having 2 separate domains responding to clients.

link|improve this answer
In actual fact, I started off with *:80 instead of default:80. As far as I can tell they're synonymous. As for using RedirectMatch 301, I don't see how that will help my situation. – Tom Wright Jul 8 '11 at 18:16
Ah - I see now - it's not only the <VirtualHost *:80>. You have a conflict of you base ServerName in the httpd.conf and the VirtualHost. Rename the base ServerName to something else (or take it out all together) and requests will be routed to your VirtualHost. RedirectMatch doesn't help the situation - just an additional consideration. – JinnKo Jul 11 '11 at 20:53
feedback

Your Answer

 
or
required, but never shown

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