Custom error pages for .php web pages? Help with htaccess

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shaxs
Forum Newbie
Posts: 2
Joined: Mon Jan 23, 2006 1:19 pm

Custom error pages for .php web pages? Help with htaccess

Post by shaxs »

A site has the following htaccess commands in it for custom error pages:

ErrorDocument 400 /error.php?code=400
ErrorDocument 401 /error.php?code=401
ErrorDocument 403 /error.php?code=403
ErrorDocument 404 /error.php?code=404
ErrorDocument 405 /error.php?code=405
ErrorDocument 500 /error.php?code=500
ErrorDocument 501 /error.php?code=501
ErrorDocument 502 /error.php?code=502
ErrorDocument 503 /error.php?code=503
ErrorDocument 504 /error.php?code=504

So, there is one error page with php code in it. Depending on the error code that Apache sends, it will display the correct error message on the website.

However, if I type in http://www.domain.com/fdfdsfsdf.html I will get the error page correctly. However, if I type http://www.domain.com/fdfdsfsdf.php, it does not use the custom page, but using the default apache not found page. Is there something else I need to add to the htaccess file to make php errors use the custom page?

I was told by the owner of the hosting company I work at that every time apache sees a .php extension, it automatically assumes it is a script and will not parse the correct error message for it.

Does anyone know how to get .php extensions to use the custom error page on an apache server? Is there something else I can add to the htaccess file to facilitate this?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

use full address:

Code: Select all

ErrorDocument 404 http://www.domain.com/error.php?code=404
Does that help?
shaxs
Forum Newbie
Posts: 2
Joined: Mon Jan 23, 2006 1:19 pm

Post by shaxs »

Jenk wrote:use full address:

Code: Select all

ErrorDocument 404 http://www.domain.com/error.php?code=404
Does that help?
Unfortunately not :(
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Right, have had a google.

You'll need to change the logic of your page..

Instead of using $_GET variables, you need to use the $_SERVER['REDIRECT_STATUS'] indice to find the error code.

So in your httpd.conf/.htaccess:

Code: Select all

ErrorDocument 404 /path/to/error/page.php
In page.php:

Code: Select all

<?php

switch ($_SERVER['REDIRECT_STATUS']) {
    case 404:
        echo "File not found";
        break;
    case 401:
        echo "Get out!";
        break;
    //etc..
}

?>
Post Reply