I decided put together a few example of rewrites that I use and like. BEWARE!!! Editing the htaccess file is not for the faint of heart. Making one small edit that incorrectly rewrites, or redirects, or changes any URL could kill your search engine rankings, and make your webmastering life miserable. Make sure you understand what you are doing, and test throughly. Be sure that no matter what no two URLs can access the same page, and make sure that any old URLs redirect to the new ones.

The Htaccess rewrite is helpful to search engines, but as search engines improve their algos and indexing capabilities the rewrites are less and less needed.

An htaccess rewrite is also needed more and more to combat the issues of the infamous duplicate content penalties that the search engines are implementing more and more. Without blabbing too much more, here’s some rewrites that I add to any site which I am contracted to conduct SEO or SEM.

Since the duplicate content penalty affects any page that can be accessed by two different URLS, I make sure that the whether the user enters www. or not before the domain name, the site always makes sure the www. is present.


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]

This Changes any URL requests for http://example.com to http://www.example.com.

The caret, ^ represents the start of the URL. The $ character represents the end of the string to be matched. The period in example.com must be escaped with a character or else it would mean something different.

The next change I make is to redirect any reference to the index file back to the domain name. So if someone tries to goto http://www.example.com/index.htm, the site will redirect to http://www.example.com/

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*index.htm? HTTP/ [NC]
RewriteRule ^(.*)index.htm?$ http://www.example.com/$1 [R=301,L]

In a nutshell, the above code kindly redirects index.htm to example.com. Some say that this is being too thorough, but I say better safe than sorry. This makes sure that the search engines will only be able to index one URL for your home page. Without the two changes above, the you run the risk of having the home page indexed at least 4 times by search engines.

Next we look at rewriting a dynamic URL to a static URL. For this example we’ll use a file called example.php

First we must make the static URL accessible when queried. To do that you must add:


RewriteRule ^([^/]*)-([^/]*).htm$ /example.php?name=$1&category=$2

This makes it so you can access the URL at http://www.example.com/toasters-5.htm

and it would be the same URL as http://www.example.com/example.php?name=toasters&category=5

But we’re not done yet. If you stopped right here, you would be blown into oblivion by the duplicate content penalty. You’d probably be ranking #995 within the week.

To make sure the old URLS tell any search engines that they have changed and are not duplicate we need to add some code which redirects the old dynamic URL to the new static one.


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /example.php?name=([^&]+)&category=([^&]+) HTTP/
RewriteRule ^example.php$ http://www.example.com/%1-%2.htm? [R=301,L]

You can test this by typing http://www.example.com/example.php?name=toasters&category=5 into your address bar. The URL should immediately change to http://www.example.com/toasters-5.htm

If the URL in the address bar does not change, then there is a problem somewhere in the file.

Your htaccess file should resemble something like this:


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]

RewriteRule ^([^/]*)-([^/]*).htm$ /example.php?name=$1&category=$2

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /example.php?name=([^&]+)&category=([^&]+) HTTP/ RewriteRule ^example.php$ http://www.example.com/%1-%2.htm? [R=301,L]

AddType application/x-httpd-php .php .htm .html