By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Creating a Custom Error Page

This article shows how to create a custom error page, and discusses its usefulness to users. The article includes both a static HTML sample page, and one in PHP.

Edited: 2014-10-30 18:43

Custom error pages are of cause optional, but they do add a degree of professionalism.

There are a few ways that you can create a custom 404 page. For static content you may want to link it in your server configuration – while for database not found errors, you should include the error page, and output the relevant HTTP Status Code. This Article only shows how to create the error page itself, check the links on the bottom for more information.

Using a Static HTML Page

You can use a static HTML page, which simply shows a standard 404 message – and that would be fine in a lot of cases – but sometimes it may be preferred to show a little more than the same error on all 404 errors. The below is an example of a static error page:

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

 <head>
   <title>404 Not Found</title>
   <meta name="robots" content="noindex,nofollow">
   <style type="text/css">
   </style>
 </head>

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

Using a Dynamic PHP Page

Using PHP allows us to include additional information, such as the exact URL requested by the user. We should however replace special characters with HTML alternatives – to avoid security problems – this can be done using the PHP htmlentities function.

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

 <head>
   <title>404 Not Found</title>
   <meta name="robots" content="noindex,nofollow">
   <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>

See also

  1. Custom Error Pages In htaccess – How to make your error page default with htaccess