1

I have installed apache2 on my ubuntu machine. As I need to work with subdomains I created a proper entry in sites-available which looks like this:

<VirtualHost *:80>
ServerName xxx.localhost
DocumentRoot /var/www/
</VirtualHost>

I also enabled mod_rewrite (and changed "AllowOverride All" in my sites-available/default file) but other than that nothing else was changed.

My .htaccess file does work, and I wanted to handle some error codes. Doing so with 404 worked pretty well, but for some reason other errors don't seem to work. I'm mostly interested in handling error 400:

ErrorDocument   400 /400.php
ErrorDocument   404 /404.php

Is there anything else I should look at? I couldn't seem to find any place where 404 are allowed while other error codes aren't.

Dan
  • 1,516
  • 2
  • 16
  • 26
  • What triggers the `400` status code? Your backend? A malformed request? – aefxx Dec 01 '12 at 01:54
  • Yes, I have some php scripts that validate input parameters. In case of error, a header(400) is issued. – Dan Dec 01 '12 at 02:06

2 Answers2

3

If the php is returning the 400 error, then php should generate the error document.

Use something like:

if( $someError )
{
  http_response_code(400);
  include("{$_SERVER['DOCUMENT_ROOT']}/400.php");
  exit();
}
Gerben
  • 16,747
  • 6
  • 37
  • 56
1

From the Apache documentation:

Although most error messages can be overriden, there are certain circumstances where the internal messages are used regardless of the setting of ErrorDocument. In particular, if a malformed request is detected, normal request processing will be immediately halted and the internal error message returned. This is necessary to guard against security problems caused by bad requests.

Try adding it directly in the httpd.conf and restart Apache.

wanovak
  • 6,117
  • 25
  • 32
  • `Context: server config, virtual host, directory, .htaccess`, so putting it inside the .htaccess should work. – Gerben Dec 22 '12 at 21:17