Customizing 404 Error Page in Apache


Customizing 404 Error Page in Apache

Everyone’s encountered the frustrating 404 error page. You follow a link, looking forward to the joy waiting for you on the other side, you get an error because the page you were looking for doesn’t exist. Maybe it was moved, maybe it was never there in the first place, but the fact is you’re left sitting there with an unhelpful error message and nowhere to go.
The best sites have found a way to lessen the aggravation by customizing their error code with a page that apologises for the mess up and offers some solutions to rectify the problem. If you want to show your readers that you care them you must set up a good Error Page.

Now, the creation of the custom page itself isn’t difficult. It is just another HTML file saved with any name you want it to have. To get your custom page to show up on a 404 error requires a little extra work. You will also need to be the one in charge of your web site, and it must be under your own domain name. You will not be able to do this otherwise, because someone else will be in charge of the domain. If you are unsure whether or not you have permission to do this, be sure to ask your web host before you proceed.
1) Using .htaccess

To begin, go to the main directory of your web site (where your main page is located) through FTP or Telnet. Look for a file there named .htaccess, it begins with the dot as though it is a long file extension. If you have the file, you will want to edit it. If you do not have the file, create a new text file and save it as “.htaccess”.

If you are creating the file from scratch, simply type the following line into the file, changing the url to the location of your custom page:

ErrorDocument 404 http://www.yoursite.com/404_error.html

Keep it all on one line, with a space on each side of the number 404. Also, this is case sensitive, so be sure to check the case of the E and D in ErrorDocument before you proceed.

If you already have the file and need to edit it, look for a line like the one above. If it is there, you can just change the url. You may want to ask your web host before doing this, just in case. If you do not have the line, add it in after everything else on its own line.

When you are done, upload your new .htaccess file to the server. Then, be sure to use the CHMOD command through your FTP or telnet program to set the permissions to CHMOD 644.

Also be sure that you have uploaded your custom page to the server, otherwise you’ll get a 500 server error or something similar. Once that is done, you can test your new error page by typing in a url that you know does not exist on your server. You should get your custom page, ready to bring in the lost visitors that may be out there.

2)Using Apache Config files

This is the simplest way to change your error page. Using command prompt go to apache configuration files ie /etc/apache2. You must be very careful while editing the config files. Edit the apache2.conffile with VIM editor.On a typical server configuration, it might look like this:

#
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
#ErrorDocument 500 “The server made a boo boo.”
#ErrorDocument 402 “/var/www/my404.html”
#ErrorDocument 404 “/cgi-bin/missing_handler.pl”
#ErrorDocument 404 http://www.yoursite.com/404.html
#

Now that you’ve found this section, you need to uncommand ErrorDocument handler that specifies the exact numeric code and the name of the file to serve up when that error is encountered. Here’s how that might well look:

ErrorDocument 404 http://www.yoursite.com/404.html

In this case, when error 404 is encountered – page or file not found – it will redirect to http://www.yoursite.com/404.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: