Jump to content
xisto Community
carny gal

Custom Error Pages Very simple error pages without php or java

Recommended Posts

Creating a custom error page without PHP

You can easily create your own custom error pages by making an .htaccess file

The .htaccess file is a simple text file which can do many things on your website. You can use it for several different things: custom error pages, banning people from your site, password protecting your directories and automatic redirects to another page among others.
Not every web host will allow you to use an .htaccess file. Usually, if your server runs Unix or Linux, or any version of the Apache web server it will support .htaccess. If you aren't sure, you can just upload an .htaccess file to your site and see if it works. It won't break anything if it doesn't :lol:

If you use Microsoft FrontPage Extensions it probably has already made a .htaccess file for you. If you do, and you don't see the file, set your FTP program to view hidden files.

Creating an .htaccess file is easy. Open any plain text editor (like notepad) and save the file as ".htaccess" (without the quotes). Don't forget the "." though. Yep. This kind of file doesn't have a first name.


Now, here is the code to put that .htaccess file to work:

Custom Error Pages
Error pages are what your visitors see when something does not work quite right on your site. For instance, if you have moved a page and forgot to change a link or if a script is not working correctly.


Custom error pages make your site look professional and also encourage your visitors to stay on your site and look for what ever it is they wanted.

You can make a custom error page for any kind of error as long as you know it's number. These are some of the most common errors:

401 - Authorization Required
400 - Bad request
403 - Forbidden
500 - Internal Server Error
404 - Wrong page

To add a custom error page for a page not found, for example, you would need to first create the custom page and upload it to your site. In this example, I'll call the page "oops.html". If you want it to work for all your directories, you should install it in your root directory.

Then you add the following line of code to your .htaccess file:

ErrorDocument 404 /oops.html

Then you would upload the .htaccess file to your root directory also.

You could also put all of your custom error pages in another directory, say, for example "errors", but then you would need to indicate the path to the directory in your .htaccess file:

ErrorDocument 404 /errors/oops.html

I hope this little tutorial helps someone. I'll be back later to add some other neat things you can do with an .htaccess file.

Notice from szupie:

Edited by szupie (see edit history)

Share this post


Link to post
Share on other sites

Well, as nobody gave any examples I think I will write how to use Error pages, even though as I remember I made a topic about how to use .htaccess files where using Error Pages were included. Just create a .htaccess file which is a hidden file and upload the file to your server, I recommend this directory - "/home/username/" so that it will still read it for all your further files. The htaccess file for Error Pages needs to contain this:

AddType application/x-httpd-php .doErrorDocument 400 /e.do?error=400ErrorDocument 401 /e.do?error=401ErrorDocument 402 /e.do?error=402ErrorDocument 403 /e.do?error=403ErrorDocument 404 /e.do?error=404ErrorDocument 405 /e.do?error=405ErrorDocument 406 /e.do?error=406ErrorDocument 407 /e.do?error=407ErrorDocument 408 /e.do?error=408ErrorDocument 409 /e.do?error=409ErrorDocument 410 /e.do?error=410ErrorDocument 411 /e.do?error=411ErrorDocument 412 /e.do?error=412ErrorDocument 413 /e.do?error=413ErrorDocument 414 /e.do?error=414ErrorDocument 415 /e.do?error=415ErrorDocument 416 /e.do?error=416ErrorDocument 417 /e.do?error=417ErrorDocument 500 /e.do?error=500ErrorDocument 501 /e.do?error=501ErrorDocument 502 /e.do?error=502ErrorDocument 503 /e.do?error=503ErrorDocument 504 /e.do?error=504ErrorDocument 505 /e.do?error=505

I will explain, the AddType, you don't necessarily need to use it, I just use the extension .do for my coding, because I like it, you can just use .php to generate your custom error pages, or you can also link to your .html files. There is also a possibility to print the error through the .htaccess file like this:

ErrorDocument 404 "<b>404 Not Found</b><br />Sorry the File Could not be found on the Server "

This is easy, isn't it ? So every time you will have an Error message your server will parse the error, you need that the file /e.do (from the example) would be in the root directory, for example your domain is http://www.mydomain.com/ so the file should be here "mydomain.com/e.do or you also can point to further directories like "/mydir/myfiles/e.do" and etc. As I know You can use ./ current Dir, ../ parent Dir and / root Dir.

I always thought, that the error pages could have a full path like "/home/user/public_html/e.do" but it can't it needs to be in the public_html. If you are using subdomains like "mysubdomain.mydomain.com" You will need to put another e.do file in your subdomain error, because the root for it is different.

Now what can the file e.do contain ? Here what my file contains:

<?php/*+=================+| Error Page File by Quatrax+=================+*//* Check Server */$url = '/home/user/privateFiles/';/* Include Root File */include $url . 'e.do';?>

If you know PHP you can easily edit it, but I have my library files in my Private directory, which is not public_html, the included file contain:

<?php/*+========================+| Error Page File for Hosting by Quatrax+========================+*//* Check Input */$code = isset($_GET['error']) ? $_GET['error'] : '404';/* Parse Error */errorPage($code);exit;# the errorPage functionfunction errorPage($code = '404') {	/* Client Error 4xx */	$e['400'] = array('400 Bad Request', 'The request could not be understood by the server due to malformed syntax. The client should not repeat the request without modifications.');	$e['401'] = array('401 Unauthorized', 'The request requires user authentication. The client may repeat the request with a suitable Authorization. If the request already included Authorization credentials, then the this response indicates that authorization has been refused.');	$e['402'] = array('402 Payment Required', 'This code is reserved for future use.');	$e['403'] = array('403 Forbidden', 'The server understood the request, but is refusing to fulfill it. Authorization will not help and the request should not be repeated.');	$e['404'] = array('404 Not Found', 'The server has not found anything matching the requested url "'.$_SERVER['REQUEST_URI'].'" and no indication is given of whether the condition is temporary or permanent.');	$e['405'] = array('405 Method Not Allowed', 'The method specified in the Request-Line is not allowed for the resource identified by the requested url "'.$_SERVER['REQUEST_URI'].'" and the response must include an Allow header containing a list of valid methods for the requested resource.');	$e['406'] = array('406 Not Acceptable', 'The server has found a resource matching the requested url "'.$_SERVER['REQUEST_URI'].'" but not one that satisfies the conditions identified by the Accept and Accept-Encoding request headers.');	$e['407'] = array('407 Proxy Authentication Required', 'The client must first authenticate itself with the proxy. The proxy must return a Proxy-Authenticate header field containing a challenge applicable to the proxy for the requested resource. The client may repeat the request with a suitable Proxy-Authorization header field.');	$e['408'] = array('408 Request Timeout', 'The client did not produce a request within the time that the server was prepared to wait. The client may repeat the request without modifications at any later time.');	$e['409'] = array('409 Conflict', 'The request could not be completed due to a conflict with the current state of the resource.');	$e['410'] = array('410 Gone', 'The requested resource is no longer available at the server and no forwarding address is known. This condition is considered permanent. Clients with link editing capabilities delete references to the requested url "'.$_SERVER['REQUEST_URI'].'" after user approval.');	$e['411'] = array('411 Length Required', 'The server refuses to accept the request without a defined Content-Length. The client may repeat the request if it adds a valid Content-Length header field containing the length of the entity body in the request message.');	$e['412'] = array('412 Unless True', 'The condition given in the Unless request-header field evaluated to true when it was tested on the server');	$e['413'] = array('413 Request Entity Too Large', 'The requested document is bigger than the server wants to handle now. If the server thinks it can handle it later, it should include a Retry-After header.');	$e['414'] = array('414 Request URI Too Long', 'The URI is too long.');	$e['415'] = array('415 Unsupported Media Type', 'Request is in an unknown format.');	$e['416'] = array('416 Requested Range Not Satisfiable', 'Client included an unsatisfiable Range header in request.');	$e['417'] = array('417 Expectation Failed', 'Value in the Expect request header could not be met.');	/* Server Error 5xx */	$e['500'] = array('500 Internal Server Error', 'The server encountered an unexpected condition which prevented it from fulfilling the request.');	$e['501'] = array('501 Not Implemented', 'The server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource.');	$e['502'] = array('502 Bad Gateway', 'The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.');	$e['503'] = array('503 Service Unavailable', 'The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay.');	$e['504'] = array('504 Gateway Timeout', 'The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server it accessed in attempting to complete the request.');	$e['505'] = array('505 HTTP Version Not Supported', 'The server, while acting as a gateway or proxy, does not support version of HTTP indicated in request line.');	/* Check, default is 404 Not Found */   $e[$code] = isset($e[$code]) ? $e[$code] : $e['404'];	/*  Remove the output buffer and turn off output buffering */	ob_get_clean(); set_time_limit(0);	/* Create Output */	$output = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD><META NAME="ROBOTS" CONTENT="NOINDEX, FOLLOW, NOARCHIVE"><META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; CHARSET=ISO-8859-1"><META HTTP-EQUIV="CONTENT-STYLE-TYPE" CONTENT="text/css"><META NAME="DESCRIPTION" CONTENT="'.$e[$code][0].'"><TITLE>'.$e[$code][0].'</TITLE><STYLE>body {	padding: 6px 24px 6px 14px;	font-family: Verdana, sans-serif;}a {	text-decoration: none;}a:hover {	text-decoration: underline;}p {	font-size: 15px;}h1 {	font-size: 32px;}small {	font-size: 11px;}address {	font-size: 13px;}hr {	border-style: dashed;	border-width: 2px 4px;	border-color: #8f8f8f;	margin-left: 0;	text-align: left;	width: 60%;}</STYLE></HEAD><BODY><H1>'.substr($e[$code][0], 4, strlen($e[$code][0])).'</H1>  '.wordwrap($e[$code][1], 64, "<BR>n").'<P>Go back » <A HREF="http://'.str_replace('http://', '', $_SERVER['SERVER_NAME']).'">the Root</A> - Main Web Site</P><P><A HREF="http://validator.w3.org"><SMALL>Valid HTML 3.2</SMALL></A><HR>'.$_SERVER['SERVER_SIGNATURE'].'</BODY></HTML>';	/* Send the Headers */	if (!headers_sent()) {		header("Content-Encoding: none");		header("Cache-Control: no-store, no-cache");		header("Cache-Control: post-check=0, pre-check=0");		header("Pragma: no-cache");		header("HTTP/1.1 ".$e[$code][0]);		header("Status: ".substr($e[$code][0], 0, 3));		header("(anti-spam-content-type:) text/html");		header("Content-Length: ".strlen($output));	}	/* Display Error */	echo $output;	return TRUE;}?>

You can edit it the way you want to, but always be sure you send the right headers, that robots would know that the page is not found on the server, to prevent them from appearing in search engines index I also added a meta tag. The ob_end_clean() function is only necessary if you are using ob_start(); Well, if you have any questions ask, because I don't want to comment on the PHP code, you can easily use simple Text. :P

By the Way, some Servers has a feature to support 404.shtml files, You can put a 404.shtml file or any other error code filename on your root Dir and if the server encounters an error, it will search for that file, but I think the first priority is usually given for .htaccess files. If you know Perl language, you can use include function to include php or any html file or even print the text, as I remember the CPanel has something similar. :lol:

I have found my old topic, so if you're interested, here is the link to it: http://forums.xisto.com/topic/87897-topic/?findpost=1064332540

I see that the the forum has edit some of my code in the quotes, I think for safety reasons :lol: it should be Content-Type not (anti-spam-content-typ..) :)

Best Regards,
Quatrux.
Edited by Quatrux (see edit history)

Share this post


Link to post
Share on other sites

I guess I didn't understand what was meant when you were looking for examples. Good job, Quatrux!When my SSI (server side includes) menues on my old site didn't show up just now when I uploaded it to my new ASTAHosting site, I was reminded of another great use for the .htaccess file.SSI will only show on a page that has the .shtml extension UNLESS you add the following lines of code to your .htaccess file:AddHandler server-parsed .htmlAddHandler server-parsed .htmAddHandler server-parsed .shtmlActually, you don't need all of them just to handle SSI but I always add them because then all HTML page extensions are covered and show up when added to your site.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.