301 redirect using htaccess

Spread the love
  • 4
  •  
  •  
  •  
  •  

301 redirect is the most efficient and Search Engine Friendly method for webpage redirection. It’s not that hard to implement and it should preserve your search engine rankings for that particular page. If you have to change file names or move pages around, it’s the safest option. The code “301” is interpreted as “moved permanently”.
Below are a Couple of examples to implement URL Redirection
Note This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.
To make the change you need to edit the .htaccess file and make the changes as described below.

Redirect to WWW

1. redirect non-www to www
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

2. To do the reverse, i.e. redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]
OR
redirect www.domain.com (case-insensitive) to domain.com
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]

Please REPLACE domain.com and www.domain.com with your actual domain name.

URL Rewriting
We’ll start off with a straight redirect; as if you had moved a file to a new location and want all links to the old location to be forwarded to the new location.

RewriteEngine on
RewriteRule ^old\.html$ http://domain.com/new.html
Though this is the simplest example possible. The structure of the ‘old’ URL is the only difficult part in this RewriteRule. There are three special characters in there.
The caret, ^, signifies the start of an URL, under the current directory. This directory is whatever directory the .htaccess file is in. You’ll start almost all matches with a caret.
The dollar sign, $, signifies the end of the string to be matched. You should add this in to stop your rules matching the first part of longer URLs.
The period or dot before the file extension is a special character in regular expressions, and would mean something special if we didn’t escape it with the backslash, which tells Apache to treat it as a normal character.
If the ‘old’ URL contains spaces, for example: /software_-_oniqua analytics Suite.asp – you need to enclose it in quotation characters
So, this rule will make your server transparently redirect from old.html to the new.html page.

RewriteEngine on
RewriteRule ^old\.html$ http://domain.com/new.html
Example for URL with spaces:
RewriteEngine on
RewriteRule “/software – oniqua analytics Suite\.asp” http://somedomain.ext/software.html

Examples
Clients Email
We need a URLs for :
1. www.clients-domain.ext/summerconcerts that links
to: http://www.clients-domain.ext/calendar/article/13520/in-tune-with-trees-summer-concerts-.html

2. www.clients-domain.ext/summerentertainment that links to: http://www.clients-domain.ext/calendar/article/10089/summer-entertainment.html

Changes we made in .htaccess file are as follows :
RewriteBase /
RewriteRule ^summerconcerts http://www.clients-domain.ext/calendar/article/13520/in-tune-with-trees-summer-concerts-.html [R=301,L]
RewriteRule ^summerentertainment http://www.clients-domain.ext/calendar/article/10089/summer-entertainment.html [R=301,L]

Another way to do the same
from: http://www.clients-domain.ext/site-map/seo/chicago-bad-credit.html
to: http://www.clients-domain.ext/component/joomap/
Redirect 301 /site-map/seo/chicago-bad-credit.html http://autocreditdealermarketing.com/component/joomap/
Implementing a 301 redirect for dynamic pages
A dynamic page is one generated by a database driven application, such as blog or forum software. A file name is appended by a query string, looking something like this:
http://www.example.com/page.php?id=13
Where a query string is used, the 301 redirect solution for static pages above will not work; you’ll need to use a rewrite solution. Using the page.php?id=13 example, here’s what you’ll need to use in your htaccess file:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=13$
RewriteRule ^/page.php$ http://www.example.com/newname.htm? [L,R=301]
In the example above the id=13 should be replaced with the query string of the page you wish to redirect and the page.php with the name of your file prior to the query string.

Examples
OLD URL : http://www.clients-domain.ext/main.taf?p=1,5,5,1
REDIRECT URL : http://www.clients-domain.ext/rent-our-facilities/weddings-and-receptions.html
OLD URL :www.clients-domain.ext/main.taf?p=2,3,1
REDIRECT URL : http://www.clients-domain.ext/childrens-garden.html
Solution :
RewriteCond %{QUERY_STRING} ^p=1,5,5,1$
RewriteRule main.taf /rent-our-facilities/weddings-and-receptions.html [R=301]
RewriteCond %{QUERY_STRING} ^p=2,3,1$
RewriteRule main.taf /childrens-garden.html [R=301]
OLD URL : http://www.clients-domain.ext/special-finance-leads/dealer-agreement-request.raw?task=vcard&contact_id=1
REDIRECT URL : http://www.clients-domain.ext/contact-us.html
Solution : RewriteCond %{QUERY_STRING} ^task=vcard&contact_id=1$ [NC]
RewriteRule special-finance-leads/dealer-agreement-request\.raw http://www.clients-domain.ext/contact-us.html? [R=301,L]
‘?’ at the end would skip the Querystring to be added at the end of the redirected URL

Implementing a redirect for URL containing spaces

Example :
OLD URL : http://www.clients-domain.ext/one two three.html
REDIRECT URL : http://www.clients-domain.ext/one_two_three.html
Solution :
RewriteRule /one\ two\ three.html http://domain.com/one_two_three.html


Spread the love
  • 4
  •  
  •  
  •  
  •  
  • 4
  •  
  •  
  •  
  •  

Leave a Reply

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