Before we look at the examples of htaccess 301 redirect let us understand overall use and purpose of 301 redirects. That should allow us to use it on correct contexts.

What does htaccess 301 redirect mean ?

htaccess 301 redirect is a mechanism to return the 301 status code as defined in RFC 2616. According to the definition of 301 redirect code, it means that the requested page has been assigned to a new permanent URI and that all future requests should be made to that new URI. The Apache htaccess file provides an easy way to return the 301 redirect status code with new location of the requested resource.

SEO significance of htaccess 301 redirect

301 redirect holds a very important key to good SEO. If your website has recently changed URLs or have moved the pages around then 301 redirect provide an efficient way of telling the search engines of the new location. Without 301 redirect a search engine could still locate new or moved content however it might consider both URLs having a copy of same content.

Simple htaccess 301 redirect example without using rewrite rule

Here is a basic 301 redirect example using the htaccess.

Redirect 301 /old_dir http://www.teqlog.com

In this example I have done a 301 redirect on all URLs that have “old_dir” in it to website main page. This is helpful if you have completely removed an old directory and want to 301 redirect all traffic to your main page rather than sending a page not found error.

Simple htaccess 301 redirect example using rewrite rule

The above can be written using the rewrite rules also as follows:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)old_dir(.*)$
RewriteRule ^(.*)$ http://www.teqlog.com/ [R=301]

301 redirect domain name to add www example (For Canonical reasons)

Canonical URL refers to picking the best url when there are several URL choices for exactly same URL. For better SEO it is important to do a 301 redirect of pages that represent same content. In this example we send a 301 redirect for URL http://teqlog.com to redirect it to http://www.teqlog.com. This is accomplished using Rewrite rules

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

The first line turns on the Apache rewrite engine. The second line checks if the URL consists of www. The third line adds the www and sends a 301 redirect such that URL without www is redirected to URL with www permanently. L flag marks this rule as last rule to process. No other rules will be processed after this rewrite rule request.

301 Redirect example using PHP

In case you are not too comfortable with modifying htaccess file then you can go ahead and write some PHP code.

if 
( 
      strstr($_SERVER['REQUEST_URI'],'old_dir' )
)
{
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.homepage.com");
exit();
}

Just place the PHP code above into any file and include it into files that need 301 redirect. If the requested URL matches old_dir then it leads to a 301 redirect to home page.