A simple default .htaccess
by Brian Turner
If you're new here, you may want to subscribe to my RSS feed. Thank you for visiting!
Introducing mod_rewrite and .htaccess
Using mod_rewrite and .htaccess together can produce some very useful effects.
What is mod_rewrite?
Simply put, a piece of software commonly installed on Linux servers, that allows the server to modify internet addresses (URLs) before the pages reach the a browser.
What is .htaccess?
Simply put, a file that you place in the main web folder of your webhosting account, to tell the mod_rewrite software what it needs to do.
Most Linux servers will have mod_rewrite installed already - but if not, ask them to install the mod_rewrite Apache module.
If you’re running your site on a Windows server, there’s a similar application called IIS, but covering that is outside the immediate scope of this article, I’m afraid.
Using mod_rewrite with .htaccess
These days, one of the more common applications for using mod_rewrite with .htaccess, is to make page URLs more friendly for search engines and human users.
For example, your browser may show the internet address of this article as:
www.platinax.co.uk/52-a-simple-default-htaccess/
This isn’t actually the real URL - the actual URL is dynamic and looks something like:
www.platinax.co.uk/index.php?c=51
Although that’s not too bad, lots of database driven websites - such as ecommerce websites, forums, directories, and other online software that needs to store information - can end up with appalling long URLs.
For example, here’s what a URL for an ecommerce site can look like normally:
&ccat=63&hidcat=65&txt_searchstring=65&txt_from=2
Not only does that look ugly for human users, but can also choke search engine spiders, and thus make it harder for people to naturally find such a page when browsing online.
Using mod_rewrite can make things simpler. The above can be turned into something like this:
/63/65-Here+is+a+best+Product+Page.html
Not only does it provide a more helpful URL for the human user, it also provides search engines with something they can actually work with.
Simple use of mod_rewrite with .htaccess
Error pages
There are simple reasons to use mod_rewrite, though. For example, you can use a .htaccess file to tell the mod_rewrite module to redirect people to an error page on the website, instead of the browser default.
For example, the following is used on Platinax, to ensure that a bad URL will lead a user to the index page, so they can navigate to the most appropriate area:
ErrorDocument 400 /
ErrorDocument 401 /
ErrorDocument 403 /
ErrorDocument 404 /
ErrorDocument 500 /
IMPORTANT: Use relative URLs when sending users to an error page, to ensure that you send appropriate 404 headers - such as for search engines - so that they know that the requested file has been removed. If you use a full URL, you may find yourself sending 200 OK headers, which tells search engines that the original URL requested *does* exist, and so they’ll keep requesting it.
Canonical Issues
Another use of .htaccess is to ensure that if someone uses the www and non-www form of your website, they are only sent to one form or another.
This can be important not simply to ensure that search engines see just one website (Google may treat them as two), but some web software on your website may have difficulty processing requests for a URL which uses an unfamiliar form.
So you can use .htaccess to ensure only one form is used, such as this:
RewriteCond %{HTTP_HOST} ^platinax\.co.uk$ [NC]
RewriteRule ^.*$ http://www.platinax.co.uk%{REQUEST_URI} [R=permanent,L]
This will make sure that users who type in http://platinax.co.uk will be redirected to http://www.platinax.co.uk.
A simple default .htaccess
The title of this article is “A simple default .htaccess”. So, let’s do that, and provide a simple and basic default .htaccess file that the majority of websites would benefit from:
Simply create a text file, then copy in the following (remembering to change the domain platinax.co.uk to your own, of course!)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^platinax\.co.uk$ [NC]
RewriteRule ^.*$ http://www.platinax.co.uk%{REQUEST_URI} [R=permanent,L]ErrorDocument 400 /
ErrorDocument 401 /
ErrorDocument 403 /
ErrorDocument 404 /
ErrorDocument 500 /
Now save that file as “.htaccess” and upload it to the public web folder (ie, public_html) of your web hosting account.
Voila! A simple default .htaccess file that will ensure that users are sent to the www form of your website, and any errors send the surfer to your index page to more easily find what they want.
If you have any kind of problems with this, feel free to ask in the coding section of the forums.
Discuss this in the Internet Business forums
Story link: A simple default .htaccess
Add to Bookmarks:
Related stories:
Leave a Reply
Previous: « The Search by John Battelle
Next: SEO is about sales »
Visited 518 times, 2 so far today




