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

Get the WebProNews Newsletter:
[ Business]

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.



Add to Digg | Yahoo! My Web

Bookmark WebProNews:

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

How to *Properly* Create a 301 Redirect in .htaccess
Top Rated White Papers and Resources
  • Flora

    Thanks – your was about the fifteenth site that I tried and you explained it in a way that worked!!

    One final question – now I have made the redirect can I take the old page down?

  • vijay

    I have One issue, please help me. My issue is I have url http://subdomain.mydomain.com and want to redirect them http://mydomin.com/folder1/index.php?user=subdomain. but I dont want to change URL in addressbar so in addressbar the url should be http://subdomain.mydomain.com. What i have to do? My .htaccess code is
    #RewriteCond %{HTTP_HOST} !^www\.mydomain.com [NC]
    #RewriteCond %{HTTP_HOST} ([^.]+)\.mydomain.com [NC]
    #RewriteRule ^(.*)$ http://mydomain.com/folder1/index.php?user=%1 [L]
    It redirect correctly, But Unfortunitly, it affect addressbar, so it’s useless for me. Please help me. What I have to do? Thanks in advance.