Skip to main content
Home
Blog

Main navigation

  • Home
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Forums
  3. Knowledge Base
  4. How-To

Custom error page 101

By ongetc , 26 February, 2017

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.

Forums
How-To

Reseller hosting (free)

Hosting24 (w/ ssh)

A2 Hosting

Monthly archive

  • September 2009 (14)
  • October 2009 (11)
  • January 2010 (2)
  • June 2010 (3)
  • October 2011 (1)
  • March 2012 (1)
  • July 2012 (1)
  • February 2017 (564)
  • March 2017 (5)
  • April 2017 (2)

Pagination

  • 1
  • Next page

Popular content

Last viewed:

  • Webman
  • MosModule: How to use snippet?
  • OpenCart
  • EmEditor
  • Comendar
  • hosting24.com
  • Coppermine
  • Hotaru CMS
  • Quick.Cms
  • PHP Nuke Evolution Xtreme

User login

  • Reset your password