Durham University

Computing and Information Services

Customised error pages

Introduction

If a visitor to your WWW pages gets the wrong URL, a 404 Not Found error page is produced. By default, this is displayed in the house style of the University's corporate WWW pages.

It is now possible to produce your own error page (for 404 errors).

So if you employ a particular style on your WWW site, you could arrange for this to be used on your error page.

How to produce a customised error page

If a visitor uses the URL community.dur.ac.uk/f.a.bloggs/research/fred.htm and the file fred.htm does not exist, the WWW server at community.dur.ac.uk will use f.a.bloggs' error page if that exists;otherwise, it will use the University's error page.

If f.a.bloggs wishes to produce their own error page, he/she needs to go to their public_html directory and create a subdirectory called errordocuments. In this directory they need to create a file called index.php.

Because the file index.php has a .php extension, the WWW server expects it to contain PHP commands. However, this file can instead contain ordinary HTML commands:

<html>
<body>
That WWW page does not exist.
</body>
</html>

However, if you know about PHP, you may want this file to contain some PHP instructions. Your PHP script will be called with a redirect_url parameter set to the URL that caused the error and with a redirect_status parameter set to 404. If you want, you could arrange for your PHP script to output these values:

<html>
<body>
<?php
echo '<h1>' . $_GET['redirect_status'] . '</h1>';
echo 'There is a problem with accessing ' . $_GET['redirect_url'];
?>
</body>
</html>

Why you should not produce a small error page

Make sure that the file containing your customised error page is bigger than 512 bytes. If you create an error page that is smaller than this, and the person visiting your WWW pages is using Microsoft's Internet Explorer, Internet Explorer will display its own error page rather than your own customised error page.

How to redirect to another WWW page

Because of the possibility of causing infinite loops, it is not possible for the PHP script to redirect to some other WWW page. Instead, you could arrange for the script to output a link giving the new URL.

For example, suppose you want to tell a visitor that URLs starting with /f.a.bloggs/java ought now to be /f.a.bloggs/papers. You could use a PHP script like this:

<html>
<body>
<h1>error page</h1>
<?php
$bits = explode("/",$redirect_url);
if ($bits[2]==="java")
{
// f.a.bloggs/java have been moved to f.a.bloggs/papers
$url = str_replace('java', 'papers', $_GET['redirect_url']);
echo 'The WWW page you were looking for has been moved. ';
echo 'It has moved to:' ;
echo '<a href="' . $url . '"><code>http://community.dur.ac.uk' . $url . '</code></a>';
echo 'Use this link to load the WWW page.';
}
else
{
echo 'There is a problem with accessing ' . $_GET['redirect_url'] . '. ';
echo 'The file containing the WWW page cannot be found.';
}
?>
</body>
</html>