Results 1 to 2 of 2
Thread: Apache Custom Error Pages
09-16-2004, 06:46 AM #1
- Join Date
- Aug 2004
Apache Custom Error Pages
Custom Error Pages with Apache
..."The server will look for a .htaccess file in the same directory as the error first, if it can't find one it will move one directory back until it finds one. Of course, if it doesn't find one then it will just use the servers or browsers default error page."...
The Apache server is probably the most widely used for hosting web sites, and is fairly easy to configure various custom error messages through the use of a
The .htaccess file
This file, if available for use on the server, can do a whole manner of things. One of the most useful applications it has is for allowing a site owner to customise their error messages, not only for each different type of error, but also for each different directory if they wish. Other uses for the
.htaccessfile include invisibly using it to redirect users to another page without the use of a meta refresh, or it can even restrict access to areas of the site (which is useful for any kind of members section). Right now however we are going to concentrate on using it to customise error pages.
Creating a .htaccess file
There is no special program that you need to be able to edit or create a
.htaccessfile, since all it is, is a normal txt file (the
.at the start of the name just indicates that it is a hidden file). Create a new text file in the usual way and edit it in a text editor (a simple editor is recommend). If you are using a Windows system for developing your web site you will soon realise that Windows does not allow you to rename a file
.htaccess. One way to get around this is to name it something obvious such as
htaccess.txtand then rename it once it is uploaded to the server.
The entries you will need to have the server open your error page are straightforward enough, and follow the format outlined just below.
ErrorDocument "3 digit error code" "text message or URL"
So, a typical
.htaccessfile used for custom errors might look something like this.
ErrorDocument 403 http://www.mysite.com/forbidden.html
ErrorDocument 404 http://www.mysite.com/cgi-bin/notfound.pl
ErrorDocument 500 "The server got very confused and couldn't help you!"
If you were visiting a site that was using this in it's
.htaccessfile then you would be taken to a page with a prewritten message on it if you weren't authorised to view the page, or sent to a script if it couldn't find the page, or given a text message if there was a server error. Notice how you are not limited to use files and scripts on your own server (although it is recommended that you do!). If you wish to have the server reply with just a simple text message, just put the message in double quotes. You should also be careful to make sure that each of your
.htaccessfile entries has its own line.
When you come to upload your
.htaccessfiles, you should be aware that it makes a difference as to where you put them. A single
.htaccessfile in the root directory will have an effect on the entire site, but it can be over-ridden by putting a different
.htaccessfile in your sub directories. The server will look for a
.htaccessfile in the same directory as the error first, if it can't find one it will move one directory back until it finds one. Of course, if it doesn't find one then it will just use the servers or browsers default error page.
09-16-2004, 10:17 AM #2
- Join Date
- Feb 2004
Very nice article. Most people don't realize what all you can do with just a .htaccess file.The Web Hosting Show - The Voice of the Web Hosting World
Think of it as talk radio mixed with Web hosting discussion for both Web hosts and Web hosting clients! New episode every Monday!