A Few .htaccess Modifications You Should Use

When putting up a website, there are a few modifications to the .htaccess file that you should consider.

1. Your site usually has two defaults that work:

  • http://yoursite.com
  • http://www.yoursite.com

Now, that's cool that you can get to it two ways. Problem is, the search engines see this as two separate websites, both with the exact same content. Problem. You definitely want to avoid the duplicate content penalty. So, to fix that, we need to add a few lines of test to your .htaccess file.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com

[NC] RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

These few lines will need to be modified with your URL, obviously. What this does is tells the server that anyone coming in to yoursite.com to automatically be forwarded to a 'different' website, www.yoursite.com. It also tells the search engine that yoursite.com no longer exists, and that all traffic for yoursite.com should be sent to www.yoursite.com. Now the search engines will no longer see duplicate content on your 'two sites'.

2. I've had this happen a few times. Sometimes clients want the site to be in 'pure HTML', meaning they don't want .php extensions at the end of their pages, they want old school .htm or .html. Which is fine, except that many times we can use PHP to make life so much easier.

Good news is, we can add just one line to our .htaccess file which will tell the server to treat any .htm or .html file as a .php file. Then we can put our PHP code into the .htm file, and it'll still parse.

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

Sweet, huh?

3. This is the big one. Mod rewrite. While I'm not going to go into this in great detail, (it deserves its own post, which plenty of others have already done), I wanted to mention that it is a great way to help in your SEO endeavors.

First, here's the code:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^member/([^/\.]+)\.html$ profile.php?u=$1 [L] Redirect 301 profile.php?u=([^/\.]+) http://www.ourdatingsite.com/member/$1.html

In this case, what we are doing is running a dating website. The site lists people's profiles, etc, and each profile goes to /member/profile.php?u=becky

In the search engine's minds, this is an ugly URL. It doesn't tell much about the member, so we've created this rule to make an imaginary URL just for the search engines that is much more friendly. This rule changes our path to /member/becky.html

Cool thing is, this page, becky.html doesn't really exist on the server. It's being faked by this rewrite. So we've created one PHP file (profile.php) which will handle any username that is passed to it, and the rewrite will create that fake page at username.html.

About 

Will Hanke owns Saint Louis' top independent Internet Marketing firm, Red Canoe Media. In addition to helping some of St Louis' most recognizable brands with their online marketing strategy, Will also is an Amazon bestselling author, speaker and teacher.

Share This Story, Choose Your Platform!

Leave A Comment

s2Member®

What's Your SEO Score?

Enter the URL of any landing page or blog article and see how optimized it is for one keyword or phrase.