I'm new to apache and httpd.conf

The problem I'm having is I need different website to go to different vhost configs and if only the IP is requested it needs to go to the root index.html

This works fine for the virtualhosts:

<VirtualHost *:80>
    DocumentRoot /var/www/html/bali-accommodation.co
    ServerName www.bali-accommodation.co

<VirtualHost *:80>
    DocumentRoot /var/www/html/balibeach.co
    ServerName www.balibeach.co

<VirtualHost *:80>
    DocumentRoot /var/www/html/dating-website.co
    ServerName www.dating-website.co

However when I just specify the IP it does to www.XXX.XXX.XXX.XXX (appends the www).

I know in the websites there is a .htaccess redirect that adds the www but I didn't want the IP to go into the vhosts config.

I tried adding this to the top of the Vhost config:

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName localhost

with above it shows the index.html perfectly but all the websites go into this catch all.

I've also tried this for the websites vhosts:

DocumentRoot /var/www/html/bali-accommodation.co ServerName www.bali-accommodation.co

I've tried the above with and without the 'www' in the first line unsuccessfully.

Is there a way to have the vhosts work for the domains and if someone just enters the IP it goes to the root index.html? /var/www/html/index.html?

thankyou so much Adam

  • Have you enabled name based virtual hosts by adding NameVirtualHost * to your config (above the VirtualHost blocks). What you've described should work (with the "default" vhost for the IP Address at the top)
    – fukawi2
    Dec 18, 2012 at 22:36

1 Answer 1


Apache searches the vhosts in the order they are defined for a match. If no other host is matched in a ServerName or ServerAlias directive Apache will serve the first vhost that was defined. You can define a catchall vhost as the last one

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName SomeName
    ServerAlias *

This will match anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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