0

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.

5
  • Check error_log for hints, it will show you which file it's trying to locate for /404.html Commented May 9, 2016 at 7:15
  • No. There is no entry since apache restart. There's only one error.log file. It does not get logged. In access.log, I see the 404 error, but no more info about what causes it. Commented May 9, 2016 at 7:21
  • Try this rule in your virtual host, RewriteEngine On RewriteRule . "Subdomain does not exist" [404] Commented May 9, 2016 at 7:27
  • That's not solving my problem, I want to have a custom, styled 404 page. Not just a text output. Commented May 9, 2016 at 7:31
  • And it causes an apache error. [404] is not accepted. Commented May 9, 2016 at 7:43

2 Answers 2

0

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> 
2
  • Hm without the redirect rule, my PHP application is executed even if this subdomain is not defined. I need this redirect to catch all requests to non-existent subdomains. I've added an example for the other VHOST definitions. Commented May 9, 2016 at 10:46
  • I guess because of my special configuration, it's not possible to have a custom 404 page here. Commented May 23, 2016 at 6:41
0

Solution:

<VirtualHost *:80> ServerName project.example.com ServerAlias *.project.example.com # CHANGED: # return 404 for all requests expect 404.html RedirectMatch 404 "^(/(?!404.html).*)" # use custom 404 error document ErrorDocument 404 /404.html DocumentRoot /var/www/html/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 

The problem is, that the "Redirect 404 /" prevents that the 404.html itself (configured with the ErrorDocument statement) can be shown. This is indicated by the error message: "Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request."

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.