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.

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 !~* ^(mydomain.com|www.mydomain.com)$ ) {
        return 444;
    }

    location  / {
        proxy_pass               http://app_server;
        ...
    }
}

How can I do the same for Apache?

UPDATE

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

2 Answers 2

up vote 2 down vote accepted

here is an example

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

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 at 20:42
    
@VladT. you're welcome) i think some things are easier in apache vs nginx. –  alexus May 8 at 20:44

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 at 19:56
1  
@EEAA Could you please elaborate on your suggestion with simple example? I'm not familiar with Apache config syntax. –  Vlad T. May 8 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 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 at 19:58
1  
@vladT. See here: httpd.apache.org/docs/2.2/vhosts Apache docs are very very good and easy to read/understand :) –  Frands Hansen May 8 at 19:59

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