Custom Error Pages For Server Using PHP

by Hiroshi on June 17, 2008

in General

This article will explain everything about error pages e.g. 404, 401,…..,425, 449, 500,….,505,…,509. The list is huge and here I have put all these error pages with their explanations, so that you may better control behavior of server when they hit some error.

Error Pages Explanations

400 Bad Request
401 Unauthorized
402 Payment Required
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable
407 Proxy Authentication Required
408 Request Timeout
409 Conflict
410 Gone
411 Length Required
412 Precondition Failed
413 Request Entity Too Large
414 Request-URI Too Long
415 Unsupported Media Type
416 Requested Range Not Satisfiable
417 Expectation Failed
421 There are too many connections from your internet address
422 Unprocessable Entity
423 Locked
424 Failed Dependency
425 Unordered Collection
426 Upgrade Required
449 Retry With
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
506 Variant Also Negotiates
507 Insufficient Storage
509 Bandwidth Limit Exceeded

Customization

404, 500, 403 and so on are error pages which you can create your own via your server as many have support but with PHP you can also built these very easily.

Here it is:

You will need some custom pages to create named as 404.php, 401.php and so on and you need a .htaccess file in root directory of your server i.e. www or public_html. That is to tell the server some command about what to do when some user encounters some error page.

Open .htaccess and write this.

ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 500 /500.php

Save .htaccess file and upload it at root directory.
This file tells the server that if user encounters a 400 error, take him to custom 400.php page.

Create 400.php, 401.php, 404.php, 403.php and 500.php and design it for your needs or you can just simply put redirection code in all these pages for user to return to home page of your website whenever he hits with error.

Redirection Code For Error Pages

<?php
header("Location: https://www.yourwebsite.com");
?>

See these pages will just contain a header and thats redirection and if some page will be called that leads to an error than it will redirect to your website custom error page. You can redirect to home page or you can redirect it to sitemap or custom error page with sitemap. Remember these pages are important for a good website to keep visitors at your website always and to not let them know that you have some file or page missing at your server for a while.

Related Posts

Previous post:

Next post: