Apache virtualhosts and default server not working as expected (serving incorrect content)

iblamefish picture iblamefish · Mar 6, 2010 · Viewed 8k times · Source

I've been playing around with this for a couple of hours and have come to the conclusion that asking someone is probably the best way forwards!

I have a fairly out-of-the-box apache install (I've added mod_security and mod_python) so not touched /etc/httpd/conf/httpd.conf

I have a number of domains all running as 's and each with its own individual config file. They are all working as expected except for the fact that any unrecognised hostname will serve the content from the first virtualhost (which I know is the expected behaviour as the content from the first virtualhost is served)

I would like to set up a default set of content to be served if someone reaches my server through an unknown hostname - but in doing so, the first site now also serves this default content which I can't seem to find a reason for.

All of the config files for the individual sites are located in /etc/httpd/conf.d/sites (so that the are not auto-included) and are then included through a file (/etc/httpd/conf.d/sites.conf) which I created - before trying to set up the default server, its content is this:

Include conf.d/sites/*.conf

I modified it to be this to try to get the default server working:

<VirtualHost _default_:80>
        DocumentRoot /home/sites/DEFAULT
        <Directory "/home/sites/DEFAULT">
                AllowOverride None
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>
Include conf.d/sites/*.conf

Just to re-iterate the problem - when the second version of sites.conf is active, the first VirtualHost also serves this content; the rest are fine. All of the sites' config files are syntactically correct and have no typos in the ServerNames.

(For reference, I have included the config file for the offending site - stored in /etc/httpd/conf.d/sites/DOMAIN.clintonmontague.co.uk.conf)

<VirtualHost *:80>
        DocumentRoot /home/sites/clintonmontague.co.uk/www
        ServerName clintonmontague.co.uk
        LogLevel emerg
        CustomLog /home/sites/clintonmontague.co.uk/_logs/access_log "combined"
        <Directory "/home/sites/clintonmontague.co.uk/www">
                AllowOverride none
                allow from all
                Options +Indexes
        </Directory>
</VirtualHost>

Sorry for the overly-long question!

Other info: Apache 2.2, CentOS 5, MediaTemple (dv)

Answer

Giacomo1968 picture Giacomo1968 · Jan 26, 2013

If what you say in your answer is correct, then try this. Note the ServerAlias line which should work instead of the PHP code:

<VirtualHost *:80>
        DocumentRoot /home/sites/clintonmontague.co.uk/www
        ServerName clintonmontague.co.uk
        ServerAlias www.clintonmontague.co.uk 
        LogLevel emerg
        CustomLog /home/sites/clintonmontague.co.uk/_logs/access_log "combined"
        <Directory "/home/sites/clintonmontague.co.uk/www">
                AllowOverride none
                allow from all
                Options +Indexes
        </Directory>
</VirtualHost>