Custom error page 101

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

If you have an error on your site and if you don't have any custom handler it will be handled by the server default configuration which might not be a very user-friendly way.
This article will show you how to can use .htaccess to have your own custom error page that handles by either HTML or PHP code.
How to use custom error page?

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 redirected to Forbidden.html

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

. For PHP page:

Create an error.php file which contains the following code and put in your web root folder then whenever you site generate error code 403 or 404 then it will be redirected to 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.