Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to return an error on wrong http_host header. For nginx I would write the following config:

server { 
    ## Deny illegal Host headers
    if ($host !~* ^(|$ ) {
        return 444;

    location  / {
        proxy_pass               http://app_server;

How can I do the same for Apache?


Though the trick with virtualhosts works, I ended up with RewriteRule:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ - [L,R=400]
share|improve this question

marked as duplicate by Shane Madden, EEAA, mdpc, MichelZ, faker May 9 '14 at 7:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 3 down vote accepted

here is an example

<VirtualHost _default_:80>
    ErrorDocument 403 /403.html
    ErrorDocument 404 /404.html

just don't put ServerName or ServerAlias inside of this VirtualHost.

share|improve this answer
Thanks a lot, you saved hours of my time. I really have no time to read Apache docs from scratch right now. – Vlad T. May 8 '14 at 20:42
@VladT. you're welcome) i think some things are easier in apache vs nginx. – alexus May 8 '14 at 20:44
This doesn't always fully address the issue. It is better to add DocumentRoot /var/www/catchall and <Directory /var/www/catchall> Order deny,allow Deny from all </Directory> – ColinM Mar 13 '15 at 21:25

Just make a VirtualHost that matches whatever host header you're interested in and then return whatever error you want.

share|improve this answer
Don't you mean VirtualHost? The NameVirtualHost defines IP/Port on which name based vhosts should be used. I'd say return the error in the default vhost and only care for vhosts that actually exists :) But maybe I understand your intention wrong? – Frands Hansen May 8 '14 at 19:56
@EEAA Could you please elaborate on your suggestion with simple example? I'm not familiar with Apache config syntax. – Vlad T. May 8 '14 at 19:57
@FrandsHansen you're absolutely right. Corrected. And I agree, just send the error from the default vhost and be done with it. – EEAA May 8 '14 at 19:57
@VladT. We don't spoonfeed answers here. Go do a bit of research, this is all very simple stuff. If you can configure nginx, you can surely configure apache. – EEAA May 8 '14 at 19:58
@vladT. See here: Apache docs are very very good and easy to read/understand :) – Frands Hansen May 8 '14 at 19:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.