0

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?

Kevin
  • 63
  • 4

2 Answers2

3

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.

LazyOne
  • 3,014
  • 1
  • 16
  • 15
1

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.

Evan Anderson
  • 141,071
  • 19
  • 191
  • 328