1. What is 404
  2. Create a .htaccess File
  3. The custom404 Page
  4. Example custom404 Page

What is a 404.

You have more than likely seen a 404 a brazillian times when poking about the net. Quite often these pages are customized to the companies site design so as to blend in. When using the standard 404 page there is little information to help the user to further navigate your site. By providing a custom 404 page a user can be shown a sitemap or other method to direct them back into your site. To begin with we will using apache and htaccess to create our 404 message and PHP to provide the content of the custom 404 page.

Create a .htaccess File

It is beyond the scope of this tutorial to describe the workings of .htaccess, so if you need more info then check out the apache docs.

Here we will use the .htaccess file to over ride the default behaviour as specified in the httpd.conf file. This line will tell the apache server to use our own custom404.php file instead of the default.

ErrorDocument 404 /custom404.php

With this in place, all pages that are not found and produce a 404 error will be directed to the custom404.php page.

The Custom404 Page

The custom404.php file is just like any other PHP file on your site. It can contain any mix of html and php etc that you choose. You have complete control over the content now. It would be a simple task to simple put in a redirection as shown here:

("Location: https://QRdvark.com");

Whilst the above method would work and the user would find themselves back on the home page, there is no information supplied to the user that the page they are looking for, or they have linked to, no longer exists. This is not really helpful to the user and the possiblity exists that a search engine will now list your homepage as a 404! With this in mind, it is a good practice to use a meta tag to prevent search engines and other bots land that may land there from following.

<meta name="robots" content="noindex, nofollow">

Similarly, a page containing only your name or logo and a "Page Not Found" heading is of little use to the user or to you as it provides no way of getting back into your site. Of course, the user may use the back button, but the user may not have come from within your site and by hitting the back button, you have now lost them.

By providing the user with information that the page they are seeking is not there followed by a method to re-enter your site is a good approach. You could provide a sitemap or other menu, along with search box so that they may search within your site for the information that require.

Example Custom404 Page

It just would not be right to have a QRdvark article without example code. Below is a simple example of what a custom404.php page might look like.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<meta name="robots" content="noindex, nofollow">
<title>QRdvark Custom 404</title>
<h1>Page Not found</h2>
<p>The page you have have looked for does not exists.
Please select from the menu or search for information using the search box below

/*** lots of code for generating a sitemap ***/

<form action="search.php" method="post">
<input type="text" name="search_text" />
<input type="submit" value="Search" />

As you can see in the snippet above, you could use any PHP functions you liked as you would for any other PHP page. This gives you amazing flexibility with database connections etc that would otherwise be lost to you with the standard 404 error page. Enjoy!