Apache2 VirtualHost – How to Disable Default VirtualHost

apache-2.2virtualhost

In apache2 by design, any http request with an unknown Host will be directed to the first loaded VirtualHost. Is there a way to disable this feature? Said differently, I want to have a web server where the user may only get to explicitly named VirtualHost definitions. Any other hostname not explicitly mentioned in a ServerName or ServerAlias line should be silently ignored.

Is this possible?

Listen 80
NameVirtualHost *

<VirtualHost _default_:*>
# Anything matching this host should be silently ignored.
</VirtualHost>

<VirtualHost *>
DocumentRoot /www/example1
ServerName www.example.com
</VirtualHost>

<VirtualHost *>
DocumentRoot /www/example2
ServerName www.example.org
</VirtualHost>

Update: As suggested below and elsewhere, silently ignoring a request might not be a good idea and perhaps breaks the RFC's for HTTP. However, since virtual hosts are designed to simulate have multiple separate physical HTTP servers, the silent ignore approach does not seem unreasonable to me. It would be the same as IP-based virtual hosting and firewalling off some of the IPs (perhaps not for all clients).

Best Answer

I'm not sure that "silently failing" is a good idea. You should give the client at least some indication of what happened. Perhaps you could send an http 410 "gone" error. Something like this should do the trick:

RewriteRule ^.*$ - [G]

In addition, you should be able to specify a custom 410 error document, which could just be a blank html page.

Related Topic