Help Visitors Find Moved Pages with a Simple Error 404 Database

Does your Error 404 page let visitors know where a page has moved to? Or does it have a generic message saying the page has moved and requires the visitor to find the page on their own? With a relatively simple database table and a few lines of code, we can enhance the error page and help customers find what they want faster.

Background

Now I know it's better to avoid moving files whenever possible. However, there are times where it feels unnecessary to maintain those old files. One website I work on, for example, had dozens of staff bio pages. To improve consistency among the pages, I moved all the staff information into a database and created a single master page to display the staff bios as needed.

Well all those individual bio pages are no longer necessary. Keeping them around to prevent broken links just increases the amount of work required to maintain the website. If they're still around and the website template changes, that's just more pages to update.

Database Setup

To let visitors know where a page has moved to, we need at least two pieces of information. We need the old website address and what to say when a visitor goes to that address. Let's also include a date to indicate when the 404 entry was added to the database and a primary key.

The database, called "error404", will look something like this:

id date oldAddress message
1 2014-01-24 /about/oldFile.pdf has been removed
2 2014-04-02 /about/bio/johnsmith.php has moved; <a href="/about/viewbio.php?id=1">view John Smith's bio</a>
3 2014-04-02 /about/bio/jakebible.php has moved; <a href="/about/viewbio.php?id=2">view Jake Bible's bio</a>
4 2014-05-01 /resources/old_page.php has been removed

Note: the date column is useful if we ever want to clean up the entries. Maybe we want to remove anything older than 10 years, for example.

Error 404 Page

With the database in place and populated, we'll take the Error 404 page from my post titled "Create Error 404 Page with .htaccess."

<html>
<head>
<title>Page Not Found</title>
</head>
<body>
<h1>Page Not Found</h1>
<p>Sorry, the page you are looking for may have been removed or had its name changed. You could try locating the page using our website navigation or website search feature. If need help locating the page, please don't hesitate to <a href="/contact/">contact us</a>.</p>
</body>
</html>

And establish a connection with the database. Note that the database connection script used for this example is described further in my post titled "End PHP Scripts Gracefully After a Failed Database Connection."

<?php
//CONNECT WITH DATABASE
require "{$_SERVER['DOCUMENT_ROOT']}/../database_connection.php";
$connect = new connect();
$mysqli  = $connect->databaseObject;
?>

<html>
<head>
<title>Page Not Found</title>
...

Then we can check if the current page address is referenced in the database.

...
</head>
<body>
<h1>Page Not Found</h1>
<?php
//IF DATABASE CONNECTION WAS SUCCESSFUL, SEE IF THE REQUESTED PAGE IS IN THE DATABASE
if($connect->success) {
     $sql    = "SELECT message FROM error404 WHERE oldAddress='" . $mysqli->real_escape_string($_SERVER['REQUEST_URI']) . "'";
     $result = $mysqli->query($sql);
}
?>

<p>Sorry, the page you are looking for may have been removed...

If it's found, display the custom message that indicates what happened to the page. Otherwise, display the standard 404 error message.

...
<h1>Page Not Found</h1>
<?php
//IF DATABASE CONNECTION WAS SUCCESSFUL, SEE IF THE REQUESTED PAGE IS IN THE DATABASE
if($connect->success) {
     $sql    = "SELECT message FROM error404 WHERE oldAddress='" . $mysqli->real_escape_string($_SERVER['REQUEST_URI']) . "'";
     $result = $mysqli->query($sql);
}
 
//IF DATABASE CONNECTION WAS SUCCESSFUL --AND-- THE REQUESTED PAGE IS IN THE DATABASE, DISPLAY CUSTOM MESSAGE
if($connect->success && $row = $result->fetch_assoc()) {
     print "<p>Sorry, the page you are looking for {$row['message']}. Please update your links/bookmarks.</p>";
     print '<p>If you have any questions, please <a href="/contact/">contact us</a></p>';
 
//ELSE...DISPLAY STANDARD MESSAGE
} else {

     ?>
     <p>Sorry, the page you are looking for may have been removed or had its name changed. You could try locating the page using our website navigation or website search feature. If need help locating the page, please don't hesitate to <a href="/contact/">contact us</a>.</p>
     <?php
}
?>

...

Final Code

To help see how all the pieces fit together, here is the entire script for the updated Error 404 page:

<?php
//CONNECT WITH DATABASE
require "{$_SERVER['DOCUMENT_ROOT']}/../database_connection.php";
$connect = new connect();
$mysqli  = $connect->databaseObject;
?>
<html>
<head>
<title>Page Not Found</title>
</head>
<body>
<h1>Page Not Found</h1>
<?php
//IF DATABASE CONNECTION WAS SUCCESSFUL, SEE IF THE REQUESTED PAGE IS IN THE DATABASE
if($connect->success) {
     $sql    = "SELECT message FROM error404 WHERE oldAddress='" . $mysqli->real_escape_string($_SERVER['REQUEST_URI']) . "'";
     $result = $mysqli->query($sql);
}
 
//IF DATABASE CONNECTION WAS SUCCESSFUL --AND-- THE REQUESTED PAGE IS IN THE DATABASE, DISPLAY CUSTOM MESSAGE
if($connect->success && $row = $result->fetch_assoc()) {
     print "<p>Sorry, the page you are looking for {$row['message']}. Please update your links/bookmarks.</p>";
     print '<p>If you have any questions, please <a href="/contact/">contact us</a></p>';
 
//ELSE...DISPLAY STANDARD MESSAGE
} else {
     ?>
     <p>Sorry, the page you are looking for may have been removed or had its name changed. You could try locating the page using our website navigation or website search feature. If need help locating the page, please don't hesitate to <a href="/contact/">contact us</a>.</p>
     <?php
}
?>
</body>
</html>

Conclusion

Now with the updated error page and database, we can get rid of all those unnecessary pages that we're maintaining just to prevent broken links. We could also dig through our server logs and/or website analytics to see if there are missing pages getting a lot of visits. If there are any, we just need to update the database.

0 Comments

There are currently no comments.

Leave a Comment