Oops! What to do when there’s an error …
June 18th, 2008 Posted in Building my websitesht what?
I’ll admit that I’m new to .htaccess files, but I’ve had to snuggle up and get much closer to them as a result of some work I’ve been doing to get my websites ready for launch.
I’ve always managed to avoid them at home, and have someone far more expert than me to take care of them at work, so I’ve been treading very carefully so I don’t mess anything up.
However, I’ve learnt quite a lot from my work in recent weeks and thought I’d pass on what I’ve discovered for other .htaccess novices.
A change of direction
The most basic use of an .htaccess file is for redirects.
I usually set these up in cpanel, but what cpanel actually does is to use that information to generate an .htaccess command.
So a straightforward redirect on this website for instance … to take you from the URL http://laptopmanpaul.co.uk to the actual destinate of this blog at http://laptopmanpaul.co.uk/it_chat/ looks like this:
RewriteCond %{HTTP_HOST} ^laptopmanpaul.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.laptopmanpaul.co.uk$
RewriteRule ^/?$ http://laptopmanpaul.co.uk/it_chat/ [R=301,L]
The reason I’ve got more embroiled in .htaccess files than I would normally want to is because a couple of my Open Source programmes have to be manually switched to search engine friendly URLS.
As an example, Mambo creates long URLS which aren’t great for search engines.
Here’s an example of a search engine ‘unfriendly’ URL:
http://www.webcumbria.co.uk/content_section/index.php?option=com_content&task=view&id=35
Here’s an example of the same URL in search engine friendly format:
http://www.webcumbria.co.uk/content_section/content/view/35/49/
You have to make a change to the .htaccess file to create that type of URL … and I’ve had to do this for Mambo and Open Realty websites.
The rules for Mambo are:
To turn SEF support ON:
1) You must run Apache
2) You must have mod_rewrite enabled
3) Your server must allow .htaccess override
4) Set $mosConfig_sef= “1″ in configuration.php
5) Rename htaccess.txt to .htaccess
Clearly you’d need to change the path to the files to reflect your own naming system, but this is what’s required to pull in those error pages at the appropriate time.
Oops! Error!
Another of the basic jobs you need to do when launching sites, is to create the customised error pages that users get if they stumble across a dead link or mis-type a URL.
It all looks a bit more professional if you produce custom error pages and keeps customers within your website rather than giving up on you and looking elsewhere.
Here are some example URLs to my error pages for this site:
http://laptopmanpaul.co.uk/error_pages/401.htm (Unauthorised)
http://laptopmanpaul.co.uk/error_pages/402.htm (Payment required)
http://laptopmanpaul.co.uk/error_pages/403.htm (Forbidden)
http://laptopmanpaul.co.uk/error_pages/404.htm (Not found errors)
http://laptopmanpaul.co.uk/error_pages/500.htm (Internet server error)
At the time of writing I’ve done no more with these pages than to send users back to the homepage, but I will be doing more with them … like adding a sitemap link … in future.
Website users would only find these pages under a particular set of circumstances … for instance, typing in a URL that doesn’t exist.
Try this crazy link for an example of how it works.
To make these error pages work correctly, you have to firstly create the pages and upload them (as above) then add the following to your .htaccess file:
ErrorDocument 400 /errors/400.htm
ErrorDocument 401 /errors/401.htm
ErrorDocument 403 /errors/403.htm
ErrorDocument 404 /errors/404.htm
ErrorDocument 500 /errors/500.htm
The cavalry arrives!
http://www.javascriptkit.com/howto/htaccess2.shtml talks you though lots of issues related to .htaccess files, and i used this to guide me through the process of reating error pages on my own sites.
If you’ve any useful .htaccess websites to share - or great tips to pass on about .htaccess iles - please use the comments form below.


