About a month ago I got a task to create an .htaccess file for a client. At the time, I had no real knowledge of the subject so I had to do research, which as many people know, can be a bit boring at times especially on subjects that we really have no interest in. I’m a coder at heart so it wasn’t too much of a pain to do the research on it and after all it was a new thing to learn which is also cool. After doing all the searching and testing I finally figured most everything out (with the help of a lot of other sites). For example, now I know how to redirect/strip away the .php/html/htm rather easy, as well as those insanely ugly URLs that we use for tracking purposes though we all know that if you use Google Analytics, you essentially eliminate the need for tracking URL’s since it keeps track of which sites referred traffic to you..

This got me thinking… I spent quite a bit of time researching and testing to get the .htaccess file to do exactly what I wanted it to do.  Wouldn’t it be easier if I did a post for all of you that will lump all my research into one central location and save you time?

First of all, if you are not running a Linux/Apache server, then this will not work for you. The .htaccess file is for these types of servers specifically. If you are running a Windows server… well switch…. Just kidding. You will be utilizing the mod-rewrite or something else similar (I haven’t had to do the research on that…yet). Now that we got that out of the way lets go on to the code/syntax of creating your .htaccess file, I’m not going to go into detail when it comes to the code except for this is what has worked for me personally on many clients including the MorePro website.

Setting up a Redirect

Non-www to www

The most common of the redirects and one of the most important at that, I can’t tell you how many times when doing a site analysis of a client’s site that I have found this to be an issue. Not having your non-www version redirect to your www version can/will create a duplicate content issue so it is very important to implement this type of redirect.

RewriteCond %{HTTP_HOST} ^SITENAME\.com$

[NC] RewriteRule ^(.*)$ [L,R=301]

This code above will redirect the non-www version of your site the www version of your site.

If you are worried about the link juice flowing properly don’t fret – with a 301 redirect all the link juice your non-www version had will now be transferred to the www version. So in essence you could see some improvement in some of you rankings because of this.

Before 301 redirect

non-301-link-juice

After 301 redirect

301-link-juice

index.html (php/htm)

This is a not so common redirect that many forget to do. While yes you can just not link to the specific index.html anywhere in your code, the file is still there. If you can go to www.yoursite.com/index.html so can the search engines making it important to redirect that as well so you don’t run into a duplicate content issue. Just like above, this may also help with your rankings as some people may have linked directly to the http://www.yoursite.com/index.html.  Having that redirect in place will help pass all the link juice the URL may be receiving onto just one single URL.

RewriteCond %{THE_REQUEST} \/index.html\ HTTP [NC] RewriteRule (.*)index.html$ /$1 [R=301,L]

If you utilize the .htm or .php, change the instance of .html above to .php or .htm.

Old pages to new pages

This is especially important if you have changed all your URLs when moving to a new site or new server or if you just happened to change the URL. You want to make sure those old pages are pointing to the new, respective URL so your user doesn’t run into 404 pages or waste all that link juice on an old page that isn’t even active anymore. This redirect will help with all that link juice passing as well as making it easier for your user to find the exact page they are looking for

Redirect 301 /oldpagename.html

Be sure to change the .html to whatever your site’s specific extension is.

Other .htaccess goodies

Stripping off unwanted/unneeded strings

Back before we were all spoiled with Google analytics we had to add ?source= or ?id=” to the end of all our URLs so we knew where the user came from. Sadly some people still live by this.  From a user standpoint, it is very ugly to look at, and from a Search Engine standpoint, this could appear to be duplicate content – although this is still under debate, as Search Engine bots are smarter and may be able to distinguish the difference between the different URLs. Regardless, it is still ugly as well as unneeded, so to keep your URLs pretty and clean you can use something like this:

RewriteCond %{QUERY_STRING} source=

RewriteRule ^(.*)$ $1? [L,R=301]

You can switch out “source=” with whatever you or your client uses currently for ids. This code will take off the ?source= and just leave the regular URL for you. For example will now look like . Now doesn’t that look so much better?

If you have multiple id strings (id=”, gid=” ect) just add that code for however many you or your client utilize. To find all the different strings that are possibly used you can use a backlink tool or any kind of URL tool such as Xenu.

Custom 404 Error Page

Having a custom 404 error page is important for a couple reasons, one being that it keeps people on your site. You don’t want people to run into a 404 page and say to themselves, “well the site/page is down I’ll go look somewhere else”, you want them to stay on your site. Secondly, 404 pages can and in fact do help with conversions. Because the user is not taken to a generic 404 page, they are kept on your site which gives them a better chance of actually finding the right page so they can convert for you. Just because you have a custom 404 page does not mean you should neglect the broken URLs, however. Use Google Webmaster tools or any other kind of broken link software tool out there and fix the broken URLs that you may have. Refer above to redirecting them to the correct URL if need be.

The code that needs to be inside your .htaccess file to point to a custom 404 pages is:

ErrorDocument 404 /error.html

This change error.html to whatever page you are using for you custom 404 page.

So how should your .htaccess file look with all these pieces of code together? Well something like this:

RewriteEngine on

RewriteBase /

RewriteCond %{THE_REQUEST} \/index.html\ HTTP [NC] RewriteRule (.*)index.html$ /$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^SITENAME\.com$ [NC] RewriteRule ^(.*)$ http://www.SITENAME.com/$1 [L,R=301]

RewriteCond %{QUERY_STRING} source=

RewriteRule ^(.*)$ $1? [L,R=301]

Redirect 301 /oldpagename.html http://www.sitename.com/newpagename.html

ErrorDocument 404 /error.html

This should do almost everything you need to be successful with your redirects and SEO needs. There are a few things you can add to the .htaccess such as php working inside html among others. Though these appear to be the most crucial when it comes to SEO, if anyone has any others that they use that they feel to be important assets to the .htaccess file, by all means share.