Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

how to configure apache2 webserver to serve pages only if request contain valid hostname/domain (one of VirtualHost)?
I've made some experiment, in my laptop hosts file I entered my server IP and some invented domain name (which doesn't exist in real, even in one of my VirtualHost file ie. To my suprise main apache server page displays instead of error message that this page doesn't exist.

So if somebody enter ANY hostname (directed by hosts file to my IP) my main webpage will appear. Strange :|

How could I prevent it? Did I miss some important apache configuration option?

share|improve this question
up vote 2 down vote accepted

The first defined virtual host for a given IP address is the default virtual host for that IP. So if you put something like this before your real virtual host, it will serve requests for nonexistent virtual hosts:

<VirtualHost *:80>
    DocumentRoot /var/www/empty

    <Directory /var/www/empty>
        Order allow,deny
        Deny from all
share|improve this answer

You need to configure Apache to use virtual hosts.

EDIT: Perhaps I misunderstood. If you have a default host (defined in httpd.conf, apache2.conf, sites-available/default, etc.) then apache will use that for any domain that does not match a virtual host (defined with ServerName or ServerAlias in a <VirtualHost> block).

share|improve this answer
So I commented ServerName and ServerAlias from main/default config file (default VirtualHost), and now I've only files with single VirtualHosts but problem still remains. – daftu Sep 11 '11 at 22:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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