ErrorDocument 404 not found in non-existent subdomain

apache-2.2errordocumenthttpd.conf

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.

Best Answer

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)