0

I am trying to get the apache server to issue a custom 404 error for invalid subdomains. The following is the relevant part of the httpd configuration:

Alias /err/ "/var/www/error/"
ErrorDocument 404 /err/HTTP_NOT_FOUND.html.var

<VirtualHost *:80> # the default virtual host
  ServerName site_not_found
  Redirect 404 /
</VirtualHost>

<VirtualHost *:80>
  ServerName example.com
  ServerAlias ??.example.com
</VirtualHost>

What I get instead is this:

Not Found

The requested URL / was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I don't understand why a URL to non-existent-subdomain.example.com produces a 404 error without custom error as shown above while a URL to eg.example.com/non-existent-file produces the full custom 404 error.

Can someone advise on this. Thanks.

Question Overflow
  • 2,103
  • 7
  • 30
  • 45
  • What's the `Redirect 404 /` supposed to be doing? – Shane Madden Sep 14 '12 at 05:59
  • @ShaneMadden, I want the page to show a 404 error. Is there something wrong? Please help. Thanks. – Question Overflow Sep 14 '12 at 07:10
  • Right, but can you clarify what that configuration was put there to do? It's invalid. Anyway, can you provide the output from Apache's error log after a request is made that returns the "bad" error page? – Shane Madden Sep 14 '12 at 07:13
  • @ShaneMadden, I suppose `Redirect 404 /` would redirect any URL that does not match any vhost listed below to a 404 error page. I have been looking at the error log every time I restart httpd server, but so far no errors produced except for the usual shutdown and startup notices. – Question Overflow Sep 14 '12 at 07:26
  • @ShaneMadden, can you explain why it is invalid? Because it does produce the "Not Found" error upon entering of a non-existent subdomain. – Question Overflow Sep 14 '12 at 07:33

1 Answers1

1

Interesting; that syntax is only documented to work with a 410 status code.

In any case, the use of the Redirect 404 / explains the inability to serve the custom error page - it's affected by the Redirect, too.

Giving the 404'ing vhost its own empty DocumentRoot is one option, since it can then "normally" 404 the requests without applying it globally to everything in the vhost (including the error page).

Alternatively.. if Redirect works for 404, perhaps RedirectMatch can, too?

RedirectMatch 404 ^/(?!err)
Shane Madden
  • 114,520
  • 13
  • 181
  • 251
  • I kind of understand why you question the validity of using 404 in `Redirect`. Although only 301, 302, 303 and 401 status codes are listed, the [manual](http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirectmatch) also states that "Other status codes can be returned by giving the numeric status code" and "If the status is between 300 and 399, the URL argument must be present, otherwise it must be omitted". Your suggestion pertaining to creating of a separate document root works, but partially only, since css and other files would be 404ed. RedirectMatch produces same result as Redirect. – Question Overflow Sep 15 '12 at 03:23
  • Upon re-reading the manual, I also understand why my custom error doesn't work. Because there is a note saying "Redirect directives take precedence over Alias and ScriptAlias directives, irrespective of their ordering in the configuration file.". Therefore, the redirect would have occurred before my `Alias` can take effect. – Question Overflow Sep 15 '12 at 03:28
  • Would it be fine to just put the custom error pages within the document root to avoid using Alias or is there another way around? Thanks for trying to help :) – Question Overflow Sep 15 '12 at 03:37
  • @QuestionOverflow The problem isn't with the precedence of the `Redirect`, it's the fact that it applies to everything. Is there some reason the `RedirectMatch` rule that I've listed won't work for you? – Shane Madden Sep 15 '12 at 07:20
  • It did worked to the same effect as a separate document root actually. I should have substituted the `err` in the RedirectMatch with the actual Alias I used. Sorry for that and thanks for your help :) – Question Overflow Sep 15 '12 at 08:22