Creating a Custom Error Page

Posted The: 24/07/2009 At: 17:06

Custom error pages are of cause optional, but i personally think that they show professionalism. Some web-developers like to simply redirect pages that ain't found, to their frontpage. A custom 404 error page is however not that hard to create, and it would also be more useful to your users, since they will actually know what happend.

There are many ways that you can create a custom 404 page, for Brugbart i simply chose to show a standard error message, with a link to the frontpage, thats likely something I'm going to improve later on, when i actually start to notice an increase in the 404 response rate.

Using a Static HTML Page

You can of cause just use a static html page, which simply shows a standard 404 message. And that would be fine in a lot of cases, there is a sample included below.

<!DOCTYPE html>
<html lang="en">

 <head>
   <title>404 Not Found</title>
   <meta name="robots" content="noindex,nofollow">
   <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
   <style type="text/css">
   </style>
 </head>

 <body>
  <h1>404 Not Found</h1>
  <p>Nothing was found on the specified location.</p>
 </body>
</html>

You can apply your own styles in between the style tags. You may still want to use an external stylesheet, because each url which the user visits is still unique, despite its the same page which is requested. This means that the entire page is downloaded each time, while an external 404 stylesheet would be cached by the browser.

Using a Dynamic PHP Page

This is the solution which i like the most, because it allows us to show additional information for the user, and even change it dynamically for each page which isn't found. This would allow us to suggest a page, which very much looks like the one that the user requested.

<!DOCTYPE html>
<html lang="en">

 <head>
   <title>404 Not Found</title>
   <meta name="robots" content="noindex,nofollow">
   <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
   <style type="text/css">
   </style>
 </head>

 <body>
  <h1>404 Not Found</h1>
  <p>Nothing was found on the specified location:</p>
  <p><?php echo htmlentities($_SERVER['REQUEST_URI']); ?></p><p><a href="/">Frontpage</a></p>
 </body>
</html>

You can read about how to configure your own error pages in htaccess, in the tutorial Custom Error Pages In htaccess

Comments: [0]

© Brugbart Webdesign