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

I got a site which I want to be returned when it is directly adressed using or but issue a 404 if the webserver gets accessed using its ip or another hostname.

I got something like the following:

<VirtualHost> #This should be used if Host is or
DocumentRoot /var/www/

<VirtualHost> #This should be used alternatively
RewriteEngine On
RewriteRule (.*) - [R=404]

How can I now set that the second entry should be used if hostname is not or

share|improve this question
up vote 2 down vote accepted

To answer your question, you can load the alternative virtualhost (second one in your file) first and it should work. (invert the order in the conf file). If the specific hostname ( or is not found then Apache will use the first loaded virtualhost that matches.

But if you use only the virtualhost you want (first one in your file - / and nothing else, doesn't Apache do what you want? It will issue a 404 if the hostname is different than the one in ServerName and ServerAlias (IP or other hostnames will be redirected to 404).

(Obs: you don't need to repeat in ServerAlias as it is already in ServerName)

share|improve this answer
No, it will just serve the VirtualHost with ServerName even though it is different from that... – Zulakis Sep 22 '12 at 14:09
Putting it first works though, thanks. – Zulakis Sep 22 '12 at 14:11
ok - good you have it working now – laurent Sep 22 '12 at 14:19

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.