PDA

View Full Version : Search engine friendly custom 404 Not Found page


Paz
04-28-2007, 08:07 AM
Hi,

This is a post with some advice for people who are upgrading their site and will be deleting old pages. Actually, we'd recommend that you don't do this, but sometimes it's unavoidable. When you do have to do make changes, it's vital to be able to 301 redirect as many deleted pages as you can, and to have a proper "404 Not Found" page for the ones you can't.

Not many people realise it but having a custom 404 Not Found page can cause you indexing problems in Google if you're upgrading your site.

The problem is that many custom 404 Not Found pages return a server header "200 OK", which means that the search engines will continue to index the deleted urls with the same content on the 404 Not Found page. This can cause duplicate content problems, particularly if you are removing a few hundred pages or more. Actually, the search engines will figure it out eventually, but it could take months and you can speed things up with the following custom 404 Not Found page.

The following 404.php 301 redirects selected pages that have been deleted, and serves a "404 Not Found" header response for the rest.

<? include $_SERVER['DOCUMENT_ROOT'] . '/globals.local'; ?>
<?
// Permanent redirection: you can have as many as you like, but check that you're not overloading the server.
$page_request_uri = $_SERVER['HTTP_X_REWRITE_URL'];
$arr_page_request_uri = explode('?', $page_request_uri);
$req_path = strtolower($arr_page_request_uri[0]);

$arr_old_pages = array();
$arr_new_pages = array();

$arr_old_pages[] = '/some-old-page01.asp';
$arr_new_pages[] = '/the-new-page01.php';

$arr_old_pages[] = '/some-old-page02.asp';
$arr_new_pages[] = '/the-new-page02.php';

$arr_old_pages[] = '/some-old-page03.asp';
$arr_new_pages[] = '/the-new-page03.php';

// do uri mapping lookups:
$new_path = ''; // default
foreach ($arr_old_pages AS $index=>$this_path){
if ($this_path == $req_path) {
$new_path = $arr_new_pages[$index];
break;
}
}
// handle all exceptions if 404.php has not been specifically requested:
if ($new_path == '' && $req_path != '/404.php') {
$new_path = '/404.php'; // default
}
# do redirection:
if ($new_path > ''){
// Permanent redirection:
header("HTTP/1.1 301 Moved Permanently");
header("Location: {$new_path}");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>404 Not Found</title>
<meta name="Description" content="The page you are looking for has been removed." />
</head>
<body>
<table width="760" height="146" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="250">&nbsp;</td>
<td width="510" valign="top">
<h1 style="font-size:24px; margin-top: 60px; ">404 Error Page:</h1>
<p>We're sorry, the page you requested cannot be found, please visit our home page, or use our sitemap to find the page you were looking for.</p>
</td>
</tr>
</table>
</body>
</html>

Thanks to our php guru Bruce Perkins (http://www.brucepeterkin.co.uk) for this one!

Cheers,
Paz.

megri
04-28-2007, 08:44 AM
Great code I will use it for my directories

Chatmaster
04-28-2007, 02:36 PM
Nice code Paz, now bring on the ASP code :D

Junichi
01-03-2008, 01:29 PM
A bit of help with ASP.NET 2.0 would be appreciated as well.

As far as I'm aware, the error-pages for the various error-codes can be defined in the web.config file located in the site root - I'm not sure if this feature was available before 2.0 ...

In any case, I've defined and created the pages I wanted to be redirected to - the 403 page works fine, but for some reason 404's refuse to redirect to the page I created for it.

Anyone have any ideas about this (apart from switching to php ... aspx is all I know ;) )

Chatmaster
01-03-2008, 02:03 PM
Junichi, do you have access to your hosting server's IIS?

Junichi
01-03-2008, 02:35 PM
Unfortunately not ... since I'm on a shared server.

If you can tell me what I need to do though I'm pretty sure I can contact my hosting provider about it - they've been pretty helpful in the past ...

Chatmaster
01-03-2008, 03:09 PM
In IIS you have a Custom headers tab (Under your site's properties). This will give you the option to point these HTTP errors (From 401 to 502) to specific pages of your choice. It is by default set to point to IIS error pages, but you can make your own and just edit the paths.

Junichi
01-04-2008, 06:56 AM
Thanks alot ... :o