Apache 2.4 ErrorDocument for multiple subdomains

errordocumentvirtualhost

we're running a large project for different customers, each has its own subdomain. Apache should not execute any script if an invalid subdomain is used. Instead, an error page should be shown.

Working:

this is out zzz-default.conf which is the last VHOST and matches all queries that are not catched by another VHOST.

<VirtualHost *:80>
        ServerName project.example.com
        ServerAlias *.project.example.com
        Redirect 404 /
        DocumentRoot /var/www/html/
        ErrorDocument 404 "This Subdomain does not exist."
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

What's not working:

ErrorDocument 404 /404.html

This file is located in /var/www/html/ and contains pure html, no scripts.

Our problem seems to be the redirect rule, but we need this to match all subdomains and rewrite to /.

If I enable this and call an invalid subdomain, I get

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
.

Anybody knows why?

Edit:
The other VHOSTs are defined as

<VirtualHost *:80>
    ServerName client.project.example.com
    Header always append X-Frame-Options SAMEORIGIN
</VirtualHost>

Include /path/to/local/client/httpd-vufind.conf

There are 13 VHOSTs defined like this and then the above zzz-default.conf is loaded.

Best Answer

The problem is the redirect rule that matches everything, including your ErrorDocument. The solution is quite simple include the redirect in the 404 response by setting remote URL as the ErrorDocument:

<VirtualHost *:80>
        ServerName project.example.com
        ServerAlias *.project.example.com
        DocumentRoot /var/www/html/
        ErrorDocument 404 http://project.example.com/404.html
</VirtualHost>