Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 "1.2.3.4".

Here would be a typical apache configuration:

Listen 80
NameVirtualHost *:80

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

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

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

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

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
I tried out solution #1. It worked as advertised. Thanks. –  Kevin Jul 31 '11 at 23:31
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

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.