» .htaccess -- Custom Error Pages v1.1 by DavH27 |
|
(Login to remove green text ads)
I’m not going to put loads of beginner rubbish in here because if you have gotten as far as wanting to use your own custom pages then you’ll know what I’m talking about in this article.
A file called .htaccess placed in certain directories of a server can have many affects such as:- custom error pages
- image hotlinking prevention
- directory listing prevention
- redirects
- enabling SSI
- deny users by IP
- password protection
- setting default directory
- adding MIME types
- prevent viewing of htaccess by users
One of the more popular ones used is the custom error pages bullet.
Your server to support .htaccess files. If somebody else runs the server and they don’t work then try emailing them asking them what they can do about it. If they are a free host then they won’t have .htaccess and won’t listen to your email. If you are running your own server and your .htaccess file does not work then you need to refer to your server’s documentation to set your server to allow usage of .htaccess files.
A text editor. The simpler the better. Winblows users just crack open that editing dynamo – Notepad.exe
Any developers that use simple text editors like Notepad can be proud =]
The best reason for this is other word processors will probably throw in extra characters and formatting that will throw your Apache server into seizures with errors foaming from it’s broadband ports … not a pretty site! (literally)
If you insist on using Word 2003 then disable word wrapping and make sure you save the file as .txt
How do I save this file.htaccess ?
Definitely not like that! .htaccess is a file that disobeys every file naming convention you can think of. It is it’s own file extension!
After you type in your .htaccess syntax as described below, save the file as ‘htaccess.txt’. Make sure you have the option enabled in your operating system that lets you view (and rename) file extensions as well as file names. Go to the htaccess.txt file and rename it. Take the ‘.txt’ bit off and add the dot ‘.’ To the beginning of the name to give you ‘.htaccess’.
Notice how the file is now in an unknown file format? Don’t try and open that file up by double clicking it – just open Notepad again and go to File > Open to open it manually.
But it already exists on my server!
Then all you need to do is edit it. The .htaccess file is strict about white space and carriage returns so do everything on one line. Just either edit the line if it exists already or add it to the lines. The .htaccess lines are in no particular order unlike files such as .HTML.
Can I have more than one file/setting for different folders?
Yes! If for instance if a main site such as ‘www.England.com’ already has a .htaccess file that does things to its folders that you don’t like then just add another .htaccess file into the folder like ‘www.England.com/London’ then these will over ride any lines in the main site.
Confused? Well imagine living next door to an off-license and having a supermarket a mile down the road. If you wanted a bottle of wine then you wouldn’t go all the way to the supermarket would you? No you’d go next door! But imagine you wanted to buy some deodorant. Next door does not sell it so you would go to the next nearest place being that supermarket!
If the nearest .htaccess file does not specify a line, then the next nearest will be referred to and so on.
Setting up those custom error pages
A simple single line will point the browser to the error page upon the corresponding error code. For instance if we wanted to set up an error page for the infamous 404 Not Found code:
Code:
ErrorDocument 404 /errors/404.html
And it’s as simple as that!
If the browser tries to retrieve a file on your server that does not exist then the above 404.html page will appear in whatever folder you’ve specified.
What if I don’t want to create a whole new page?
Then change the above line to this:
Quote:
ErrorDocument 404 “<H1>Cannot find your page!</h1>Please go back and try another link.
#Note how there is no closing “ character?
There shouldn’t be one so don’t use it!
|
Why am I using 404?
There is a list of different codes, some not actually being error codes as such. Some can’t be used and some will never be referred to anyway so creating a page will be wasted. Below is a list of the pages:
Quote:
Successful Client Requests
200 OK
201 Created
202 Accepted
203 Non-Authorative Information
204 No Content
205 Reset Content
206 Partial Content
Client Request Redirected
300 Multiple Choices
301 Moved Permanently
302 Moved Temporarily
303 See Other
304 Not Modified
305 See Proxy
Client Request Errors
400 Bad Request
401 Authorization Required
402 Payment Required (not used yet)
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable (encoding)
407 Proxy Authentication Required
408 Request Timed Out
409 Conflicting Request
410 Gone
411 Content Length Required
412 Precondition Failed
413 Request Entity Too Long
414 Request URI Too Long
415 Unsupported Media Type
Server Errors
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout]
505 HTTP Version Not Supported
|
I could go into detail of what each one of these codes mean but that would be a whole new article in itself.
There are however, 5 codes that I strongly recommend you create pages for: 400, 401, 403, 404 and 500.
400 because it’s one of those generic kind of errors that browsers get by doing something funky to your URL or scripts.
401 because the user is trying to access a protected folder of your site without the proper credentials i.e. password. More details on this can be covered in another article in the near future.
403 because the browser has requested a file with disallowed permissions.
404 for those oh-too-often times when the user clicks on a broken link pointing to a file that does not exist.
500 because sometimes a script generates an internal server error and your users have to be notified that the script is not working.
I’ve made my .htaccess file, now what?
Now you need to upload the file to your server. You should have really planned your htaccess beforehand if you wanted different things to happen to different folders but for default’s sake, upload this to your site’s root folder. When you FTP the file, make sure you do it by ASCII. This is very important! I’m not sure why but it does some screwy things to the file if you upload using binary.
How will I know when these error pages are being shown?
This section is still being investigated. I would like to introduce a method of which logs an error in a .txt file on your server saying what error what generated from which page. I would also like to tell you guys how an email is automatically generated and sent to the admin to notify them of the error. This will allow web designers of large scale sites to keep them fully functional and free of errors.
For now I can only suggest you put in either an email link, and email form or, if you know how to do it, a form that posts an entry into either a full database/flat file database.
I just can’t rename my file to ‘.htaccess’
If your OS won’t let you rename your file to .htaccess then try uploading the file to your server before trying to change the name using the FTP program.
If you run your own server or somebody else is running a server dedicated to you then you can change the configuration of the Apache software to recognise the .htaccess by another name. Simply go into the Apache directories and open up the configuration .config file. Look for the line that reads,
Code:
AccessFileName .htaccess
Remember to restart Apache after config file changes.
Edit it according to the Apache manual.
http://httpd.apache.org/docs/mod/cor...accessfilename
How do I set Apache to read .htaccess files?
If the server does not work on these files as if they never existed then that is probably because they are simply being ignored. Go into Apache’s config file again and look for the following line and modify it according to the below example. Remember to restart Apache after config file changes.
The options for setting the AllowOverride directive can be found at the Apache manual.
http://httpd.apache.org/docs/mod/cor...#allowoverride
What if I don’t want search engines such as Google to pick up my error pages and list them?
Incorporate a ROBOTS.txt file in your server root directory. This file will be read by spider bots – an automated program sent by a search engine to index new pages and the changes of older indexed pages.
Article version 1.1
If you ahve an questions, suggestions, critisisms, remarks, gestures, addictions to declare or anything else you feel I should know about, please don't hesitate to contact me through the forum.
|
|