May

11

Human creations are like ourselves - imperfect. The occurrence of error, whether caused by the surfer or by programmer, is inevitable. But it’s our job to do the damage control, the best thing to do when an error occurs is to redirect browsers that experience a 404 error (Not Found) to the file “error.php” located on the root of the server. With a little PHP coding you can set up this file to handle all error codes which will make them easier to manage.

The complete (and very long) list of errors is available here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Some of the most common errors you’ll probably want to make entries for are:

400 - Bad Request
401 - Unauthorized
403 - Forbidden
404 - Not Found
500 - Internal Server Error

.htaccess

To catch those errors, you would add the following to .htaccess for the domain you’d like to configure:

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

error.php

Then, in error.php, add something like the following. This particular example is made for a wordpress site, and if someone goes to http://www.example.com/Foo and the page’s not found, they are redirected to the search result page http://www.example.com/index.php?s=Foo. Anything after the last “/” is assumed to be a search term they were trying to get to.

<!--p
// this is especially useful with error 404 to indicate the missing page.
$page_redirected_from = $_SERVER['REQUEST_URI'];
$server_url = "http://" . $_SERVER["SERVER_NAME"] . "/";
$redirect_url = $_SERVER["REDIRECT_URL"];
$redirect_url_array = parse_url($redirect_url);
$end_of_path = str_replace("/", "", $redirect_url_array["path"]);

switch(getenv("REDIRECT_STATUS"))
{
	# "400 - Bad Request"
	case 400:
	$error_code = "400 - Bad Request";
	$explanation = "The syntax of the URL submitted by your browser could not be understood.";
	$explanation .= "Please verify the address and try again.";
	$redirect_to = "";
	break;

	# "401 - Unauthorized"
	case 401:
	$error_code = "401 - Unauthorized";
	$explanation = "This section requires a password or is otherwise protected.";
	$explanation .= "If you feel you have reached this page in error, ";
	$explanation .= "please return to the login page and try again, ";
	$explanation .= "or contact the webmaster if you continue to have problems.";
	$redirect_to = "";
	break;

	# "403 - Forbidden"
	case 403:
	$error_code = "403 - Forbidden";
	$explanation = "This section requires a password or is otherwise protected.";
  	$explanation .= "If you feel you have reached this page in error, ";
	$explanation .= "please return to the login page and try again,";
	$explanation .= " or contact the webmaster if you continue to have problems.";
	$redirect_to = "";
	break;

	# "404 - Not Found"
	case 404:
	$error_code = "404 - Not Found";
	$explanation = "The requested resource '" . $page_redirected_from . "'";
	$explanation .= " could not be found on this server.";
	$explanation .= "Please verify the address and try again.";
	$redirect_to = $server_url."?s=". $end_of_path;
	break;

	# "500 - Internal Server Error"
	case 500:
	$error_code = "500 - Internal Server Error";
	$explanation = "The server experienced an unexpected error.";
	$explanation .= "Please verify the address and try again.";
	$redirect_to = "";
	break;
}
-->

<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt-->

<!--p
	if (strcmp($redirect_to, "") != 0)
	{
-->

<!--p
	}
-->
<h1>Error Code <!--p print ($error_code);--></h1>
The <a href="http://en.wikipedia.org/wiki/Uniform_resource_locator" onclick="javascript:pageTracker._trackPageview ('/outbound/en.wikipedia.org');">URL</a> you requested was not found. <!--P echo($explanation);-->

<strong>Did you mean to type <a href="http://www.blog.highub.com/wp-admin/%3C?php print ($redirect_to); ?>"><!--p print ($redirect_to);--></a>?</strong>
You will be automatically redirected there in five seconds.

You may also want to try starting from the home page: <a href="http://www.blog.highub.com/wp-admin/%3C?php print ($server_url); ?>"><!--p print ($server_url);--></a>
<hr />

<em>A project of <a href="http://www.blog.highub.com/wp-admin/%3C?php print ($server_url); ?>"><!--p print ($server_url);--></a>.</em>


Similar Posts

Comments

Name (required)

Email (required)

Website

Speak your mind

1 Comment so far

  1. m0dk1d on May 21, 2008 10:36 am

    very comprehensive, thanks!

Sponsors




Links