How to use custom error page?

Pick Language to Auto Translate:
AR | BG | CA | CS | DA | DE | EL | ES | FI | FR | HI | HR | ID | IT | IW | JA | KO | LT | LV | NL | NO | PL | PT | RO | RU | SK | SR | SL | SV | TL | UK | VI | ZH | ZH-TW

You can handle custom error page either with plain HTML or PHP code but both use .htaccess to do the redirect of the error code.

. For plain html page:

If you want to have a custom error page of you own, add one to your .htaccess file with this format: ErrorDocument [error number] [URL of custom page] (Note: One line correspond to one error number) and create the correspondence file in the same folder.

For example: 404 will redirect to notFound.html, 403 will be redirect to Forbidden.html


## In .htaccess file
ErrorDocument 403 /Forbidden.html
ErrorDocument 404 /notFound.html

. For PHP page:

Create error.php file which contain the following code and put on your web root folder then whenever you site generate error code 403 or 404 then it will be redirected by this file.


## In .htaccess file
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php

<?php
## error.php file
$status = $_SERVER['REDIRECT_STATUS'];
$codes = array(
403 => array('403 Forbidden', 'The server has refused your request.'),
404 => array('404 Not Found', 'Page requested was not found.'),
);

$title = $codes[$status][0];
$message = $codes[$status][1];
if ($title == false || strlen($status) != 3) {
$message = 'Unknown status code!';
}
echo '' . $title . '' . '' . $message . '';
?>

Note: This sample only use 403 and 404 but you can add more error code to the array to be handled by this error.php file.