Creating Custom PHP 404 Error Pages
Ray Hunter on 2002 May 30
    

Have you ever came across a 404-error handler page while surfing the web? A server displays these pages when a browser sends an incorrect HTTP request to a web server. Now, have you ever seen 404-error pages that have the same look and feel as the web site you are on instead of those boring generic Apache or IIS ones? In this tutorial I am going to show you how to create and customize your own custom 404-error pages

.

Creating the 404-error page
First, I will show you how I created my own custom error page. Here is the code for the page.


<?php


    
// You might remember this from
    // my article on Generic branding
    
include( 'header.php' );

    echo 
"We apologize for the inconvenience, but the page you"
        
" are requesting is no longer available."

    
echo "You can click on the back button to go back to the previous page"
        
" or you can click on the logo above to go to the home page."

    
include( 'footer.php' );


?>


I saved this file as error.php.


That was easy enough! If you have read my article on generic site branding with PHP then you probably remember the "includes" I have there. The first include gives us the logo and navigation for my site while the second one gives the end of the page.

I just echo out a simple message to the user, announcing that the requested page is no longer available.



Apache and your custom 404-error page
In Apache, we only need to change some lines of code in the httpd.conf configuration file. Let me show you what lines you need to edit to get you rolling.

1. Open up httpd.conf in your favorite editor (vi or emacs anyone?)
2. Search for ErrorDocument 404
3. Uncomment the line and then add the URL to the error file you made.
4. Save the file.
5. Restart Apache.

Like this (httpd.conf):




# 2) local redirects
ErrorDocument 404 http://www.yourdomainname.com/error.php



There are many different options to use with the ErrorDocument tag, however for ease of this tutorial use this for now.

NOTE: I like theory and here is a little for ya...

When the server gets a request it can't honor, this line tells the server to do a redirect and send the browser to the specified URL. So the browser never really receives a 404-Error, it just gets a redirected to the URL you specified.



IIS and your custom 404-error page
So, you prefer to use IIS for your web server. I am using IIS 5 so I do not know if the same scheme works for all versions. (Probably not, but ask Bill to support you on this). Here, it is...

1. Click Start -> Settings -> Control Panel -> Administration Tools -> Internet Information Server
2. Window opens.
3. Click down until you come to Web Sites.
4. Right click and select Properties.
5. Click on the tab Custom Errors.
6. Window opens.
7. Scroll down to the 404 error entry.
8. Select it.
9. Click on Edit Properties
10. Then just add the absolute path to the error.html file.
11. Restart IIS for the hell of it...

For some mysterious reason, IIS will not allow you to have a .php file for the error file. I suggest just creating a custom .html file and setting that as your custom 404-Error page.

And if anyone has a work-around for this, please let me know so I can add it to my list of the vanishing species. You can guess by now what a big IIS fan I am.



Custom 404-error pages for your site
Now, for those of you that have a hosted web site all is not lost. If that is the case and they are using Apache as the web server, you can still create your own custom 404 error pages.

1. Start up your favorite text editor on your computer
2. Type the following:

ErrorDocument 404 http://www.yourdomainname.com/error.php
where www.yourdomainname.com is your domain.

3. Save the file as htaccess
4. FTP into your domain using your administrative login and password.
5. Go up two directory levels until you see the following directories /logs, /web, /users.
6. Go down one directory level to the /web directory
7. Upload the file htaccess to that directory. Make sure to upload the file in ASCII not BINARY.
8. Then right click on htaccess, and select rename and type .htaccess then hit enter

Note: Once you create the .htaccess file, you may not see it in your Telnet of FTP clients. The "." makes it a hidden file on our system.

This is all you need to create a custom 404 page.


Summary
When you have set this up on your web server, you can test it out by typing in any URL that does not exist on your web server. Voila! You should see the brand new 404-error page that you just created. I would suggest playing around with it and becoming familiar with your web server.

I like to use Apache to redirect error requests back to my main index.php page. Try it and see...that way users do not see a huge 404-Error page.

If you are using Microsoft IE and you do not see your custom 404 error page, let me say that Microsoft has taken the liberty to having their own IE 404 error pages come up instead of some sites. So, if you want your custom 404 page to be displayed instead of IE's 404 error page then you will have to create your custom 404 page bigger than 512 bytes. However, if you have Apache and following the directions from above you will not have a problem with IE because you are setting it up as a redirect instead of an HTTP error request.


Ray Hunter
Tentatively planning to Open Soon! (no dates ...) // Doing heavy development now...