Forum

ErrorHandler not working with .php files

Jamie 'JackHerer' Lindsey
15 February 2017, 01:30
Hi folks, I am having an issue handling HTTP errors (HTTP/1.1 404, 500 etc) with Hiawatha. I have created a simple PHP error handling script that redirects a user to a custom error page, by using the configuration option
ErrorHandler     = 404:/php/error.php?error_code=404
.

Here is my PHP error script:

<?php

header('Location: /error_pages/http-'.$_GET['error_code'].'.html');

?>


Which is obviously really simple but works well, but only on errors with .html files. If I try to request a .php file that is not found I don't get the custom 404 error page, instead I was getting the PHP response of a very simple 'File not found' message, so after reading around I found that I need to include the
TriggerOnCGIstatus = yes
configuration option, which, when I tried to request a non-existent .php file I get Hiawatha's default 404 error page - not my custom error page! (With .html files it will work as desired either way)

My best configuration so far is to have my configuration option as
ErrorHandler     = 404:/php/error.php?error_code=404&error_message=Not%20Found
and my error.php like so:

<?php

header('Status: HTTP/1.1 '.$_GET['error_code'].' '.$_GET['error_message'].'');

?>
<html>
<head>
<meta http-equiv="refresh" content="0;URL=error_pages/http-<?php echo $_GET['error_code']?>.html">
</head>
<body>
</body>
</html>


but this is a crappy option as it brute redirects the page and many people (as I do) set up the browser to stop auto redirects.

Any help on this? I have basically tried;
1. Using
TriggerOnCGIstatus = yes

2. Not using
TriggerOnCGIstatus = yes

3. Everything as above
4. Using PHP's
include('error_pages/http-'.$GET['error_code'].'.html');


It would seem nothing works, except the
http-equiv="refresh"
, but I suspect that there is a fairly simple way to resolve this issue.

Many thanks in advance
Hugo Leisink
17 February 2017, 14:00
Hi Jamie, I just tested the ErrorHandler and TriggerOnCGIstatus option and it works. Instead of redirecting, try printing a simple message via /php/error.php first. Just to see if in what step it goes wrong.

Why do you use a PHP script to redirect the user anyway? Why not use
ErrorHandler = 404:/error_pages/http-404.html
Jamie 'JackHerer' Lindsey
18 February 2017, 13:11
Hey Hugo, thanks for replying, I was trying to figure out a way that would handle all errors via error.php with the given parameters. I did as you said and I can get a custom message to display, I guess I will need to create a much more complex .php error handling script. Your alternate suggestion works well so I will just use that method for the time being, again thanks! BTW I'm just about to download and rebuild v10.5 I hope it goes as easy as previous builds.
This topic has been closed.