How can I get Apache to not respond to an IP-only request?

I have several web sites, all set up in http-vhosts.conf with their respective ServerName and DocumentRoot, they are working fine and dandy.

But when I visit the server's IP address URL, I get back the files in the first virtual host, rather than Apache serving from where I would expect, the http.conf file's DocumentRoot.

How can I configure Apache so visiting the server by IP address, whether external or loopback, does not return the first VirtualHost?

Here's the first VirtualHost, which I'm currently getting back as the response if I visit the web server by IP:

<VirtualHost *:80>
    ServerName "delaneatallent.com"
    ServerAlias "www.delaneatallent.com"
    DocumentRoot "/Library/WebServer/Documents/delanea"
</VirtualHost>

Using Apache 2.2.14 on OS X, if that matters.

:wq


Solution 1:

In short, you can't - that's how Apache works. If you are listening on that IP (as per Prix's comment) then Apache will use the first virtual host as defined when serving up an IP request.

If this is undesirable, then consider changing the first virtual host to be what you actually want served if it's by IP. If that is not what you want, then consider putting in redirect as the first virtual host to make it go somewhere you do want.

# default
<VirtualHost *:80>
  RewriteEngine on
  RewriteRule ^/(.*) http://othersite.com/$1 [L,R]
</VirtualHost>

You can't stop Apache from listening on that IP, but you can make it do something else that does do what you want with a little creativity.

Solution 2:

What I did to remedy this was to do a default page for /var/www and that serves the default site you see when you navigate to my server by IP or unknown url related to a site on my server.

The actual sites that I host are in the sites-enabled folder with the default site configuration, all name based and pointing to a different directory.

Long story short, navigate to my server by its IP, you get the default error page.