3

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>

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

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

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
</VirtualHost>

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

1
  • 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

4

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 *
</VirtualHost>

This will match anything.

You must log in to answer this question.

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