Articles » Useful 404 error pages

After reading a few recent articles on custom 404 error pages we decided to come up with a similar system for ourselves.

It seems many web designers and web design company's for that matter still don't use or acknowledge the importance of 404 error pages. For the website user a simple error page can make all the difference, rather than being presented with a blank page offering no explanation.
So after researching this we set of to design a dynamic page explaining to the website user a reason why they have arrived at the error 404 page. This got us thinking – we also would benefit knowing this information so the website admin's could fix any recurring issues.

First things first
Before we go any further we need to enable the ErrorDocument directive in the .htaccess file.

1
ErrorDocument 404 404.php

We have named our file as 404.php although you may choose something different. If you are using a different folder other than root for your error page you must specify this in relative form in your .htaccess file.

1
ErrorDocument 404 /name_of_your_folder/404.php


Data collection

We need to collect data so we can establish where the problem is and why they have been redirected to the 404 error page. We use the following two lines of code to get the referring URL and the active URL.

1
2
$referer = htmlentities(mysql_real_escape_string($_SERVER['HTTP_REFERER']));
$active = htmlentities(mysql_real_escape_string($_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']));

Using this data we can assume that if the variable $referer is blank that the user has either mistyped the URL or has an out of date bookmark referring to a nonexistent or deleted page.

If $referer is not blank we can then check if the link is from another internal link from the website, from a search engine or from another website entirely.

To determine this we use the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
// return name of search engine if link is from one
// more search engines can be added here.
$searchEngine = "";

if(eregi('google',$referer)) {
    $searchEngine = 'google';
}
if(eregi('yahoo',$referer)) {
    $searchEngine = 'yahoo';
}
if(eregi('msn',$referer)) {
    $searchEngine = 'msn';
}
if(eregi('alltheweb',$referer)) {
    $searchEngine = 'all the web';
}
if(eregi('askjeeves',$referer)) {
    $searchEngine = 'ask jeeves';
}
// mistyped URL or out of date bookmark
if(empty($referer)) {
    $message .= "The page '".$active."' Does not exist on our site. You may have used an out of date bookmark or mistyped the URL. Details of this error have been passed onto our site admins.";
    
}
// user has followed a bad link
else {
    
    // check if the link is internal
    if(eregi($_SERVER['SERVER_NAME'],$referer)) {
        $message .= "Sorry the page you are trying to view '".$active."' Does not exist.<br />This is possibly due to a broken link on our website. Details of this error have been passed onto our site admins.";
    }
    // check if the link is from a search engine.
    elseif(!empty($searchEngine)) {
        $message .= "Sorry the page you are trying to view '".$active."' Does not exist.<br /> This is possibly due to an old link indexed with a search engine. Details of this error have been passed onto our site admins.";
    }
    // else link is from another website.
    else {
        $message .= "Sorry the page you are trying to view '".$active."' Does not exist.<br />This is possibly due to a broken link on another website. We will attempt to contact the owner of the website you just came from. Details of this error have been passed onto our site admins.";
    }
}

// we then add the following code to send all the details to our database.
// The details are checked daily by our website admins.
$sql = "INSERT INTO 404_logs VALUES (
        '',
        '".$referer."',
        '".$active."',
        '".time()."',
        'unchecked',
        '".$_SERVER['REMOTE_ADDR']."'
    )";

mysql_query($sql) or die(mysql_error());
?>

Long lines of code are broken up with -> remove this when using the code on your website.


Captcha Image