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.

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).

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
I ended up discovering this solution on my own as well. You should add lines for loading mod_rewrite and "RewriteEngine On" noting that they may not be necessary. They were for me. :) –  Harvey Feb 20 '10 at 16:48
1  
Also, since we're just saying Gone or Forbidden and more importantly, the "-" prevents substitution, the regex can be much simpler: RewriteRule . - [F] –  Harvey Feb 20 '10 at 16:53

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.