I was just trying to catch a 404, page not found, error from apache. The documentation made it look easy. Just add the ErrorDocument directives to your .htaccess file
ErrorDocument 404 /error.php
This should redirect the browser to the error.php file whenever the server gets a file not found error.
But it wasn’t working!
At first I thought the path wasn’t being resolved properly because I was getting an additional 404 error saying “a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.”
So I tried making the path absolute
ErrorDocument 404 http://localhost/path/to/site/error.php
This kind of worked. But I wanted to know what page the user had tried to access that caused the 404. Apparently there is supposed to be a server variable called “REDIRECT_URL”. But PHP kept saying it wasn’t present. So after much googling, I read that…
None of these will be set if your ErrorDocument is an external redirect (i.e., anything starting with a scheme name like http:, even if it refers to the same host as the server).
Doh!
Then I realised that I was right the first time. The path was not being resolved properly. Apparently a local path needs to be relative to the server root, not to the directory the .htaccess file is in. So…
ErrorDocument 404 /~username/path/to/site/error.php
This worked a treat
I got my custom error message and I got the REDIRECT_URL server variable.
Happy, I am.
BTW, Server environment is Apache/1.3.33 (Darwin) PHP/5.0.4 on OS X 10.4.8