A Few .htaccess Modifications You Should Use

Table of Contents

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 [fusion_builder_container hundred_percent=”yes” overflow=”visible”][fusion_builder_row][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][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.

[cp_slide_in display=”inline” id=”cp_id_8e7bd”][/cp_slide_in]

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.


Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts