I have a fairly basic question. Please be patient with me.

As an example, say I host "www.example.com" and "www.another-example.com" on a single server with an ip address of "".

Here would be a typical apache configuration:

Listen 80
NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.example.com
# Other directives here

<VirtualHost *:80>
ServerName www.another-example.com
# Other directives here

How do I return a 404 error code for a request to "" without blocking all websites on the server?

Could you please include an example of an apache configuration directive I should be using?


First virtual host definition will be used as "catch all" in case if domain name in not known to Apache.

I know 2 solutions:

  1. Create one more <VirtualHost> pointed into empty folder and make it listed above all other virtual host definitions.

  2. Using mod_rewrite create a rule in first virtual host to return 404 response if %{HTTP_HOST} is an IP address.

Approach #1 is more logical and widely used. Approach #2 I would not use unless under some special circumstances.

  • I tried out solution #1. It worked as advertised. Thanks. – Kevin Jul 31 '11 at 23:31

The first defined virtual host will answer queries for the bare IP address. You can also just include a named virtual host for the IP address itself and point that at a directory with whatever you'd like to return in it.

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.