Tuesday, November 5, 2024

How to *Properly* Create a 301 Redirect in .htaccess

Last weekend, when I moved my blog to this new domain, after moving the files from their old location at http://www.infohatter.com/blog/ to their present location, I needed to set up a redirect to ensure that traffic following links pointing to the old location would still end up at the right posts.

Additionally, I knew that I wanted to use a 301 Redirect, since that is commonly held to be more Search Engine Friendly than a 302 Redirect. For those wondering why, a 301 redirect indicates to the visitor or bot that the page has moved permanently to it’s new location, as opposed to a 302 redirect, which indicates that the move is only temporary.

Now, I had never set up a redirect before, so the first thing I did was to Google it. I found a bunch of sites teaching about how to do redirects, but they all made either one or both of the following assumptions:

  • I had access to the server httpd.conf
  • I only wanted to redirect a single page

I am on shared hosting, thus do not have access to Apache’s httpd.conf. Instead I knew that I had to perform the move in my sites .htaccess file.

Since I wanted to move an entire site rather than a single page, using the instructons given for rewriting a single URL to point to a new location would have become extremely tedious given that I had nearly 500 active URLs at the new location.

Eventually, frustrated by the lack of instruction on the web, I finally turned to #apache on irc.freenode.net, where some helpful people got me finally set up.

How to 301 Redirect an Entire Site in .htacess

If you wish to have all the URL’s in an existing site redirect to your new site, include the following code in the .htaccess file in the root of the old site.

Redirect 301

So, in my case, I added the following line to the .htaccess file in the root of infohatter.com

Redirect 301 /blog http://www.caydel.com

Now, any URL which points to my old blog location is now forwarded to it’s corresponding URL in the new blog. For example, http://www.infohatter.com/blog/monetize-your-blog-3-selecting-your-advertising-strategy/ now redirects all the visitors to http://www.caydel.com/monetize-your-blog-3-selecting-your-advertising-strategy/

301 Redirect for a Single URL in .htaccess

If you have a specific set of pages you would like to move, while leaving the others intact, you can use the following 301 redirect in your .htaccces. Notice that the syntax is the same as we used above to send a whole subdirectory to a new url. This form will also keep any arguments passed along with the URL as a GET or POST request, so that they are available to the redirected page.

redirect 301 /old/old.htm http://www.you.com/new.htm

301 Redirect to your www

I feel somewhat obligated to include this, since this is perhaps the most important redirect in existence, and EVERY SITE SHOULD USE IT.

Many webmasters do not realize that they likely have their entire site duplicated. Google considers www.example.com to be a different site than example.com. The www’ is considered tp be a seperate subdomain,

This presents a problem because if you have links pointing to both versions, Google will split the attributed trust from these links between the two copies of your site. This will result in lower search engine rankings, and lower search traffic to your site. Now this can be eliminated by implementing this simple 301 redirect in your .htaccess.

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

Good Luck – remember that you always want to use a 301 redirect instead of a 302. Always make sure you 301 redirect your non-www’ version of your website to the www’ version for the best Google results.

Comments

Tag:

Add to Digg | Yahoo! My Web

Bookmark Murdok:

Did you like this article? Keep up with Brian’s expert search optimization tips and tricks at http://www.caydel.com

Related Articles

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles