301-redirect-drupalIf you like to redirect a your Drupal site  from http://example.com to www.example.com or the vice versa www.example.com to http://example.com then this tutorial is for you.

What is the Advantage of Doing that ?

If your Domain is like http://example.com then search engines will consider that http://example.com and www.example.com are different domains hence it will consider your content as duplicate in any one of the URL which  will decrease your website’s SEO reputation.

301 Redirect form http://example.com to www.example.com

For achieving this you need to edit .htaccess file which will be in your root domain.

At line 84 you will find these codes

  # To redirect all users to access the site WITHOUT the ‘www.’ prefix,
  # (http://www.example.com/… will be redirected to http://example.com/…)
  # uncomment the following:
  # RewriteCond {71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}{HTTP_HOST} ^www\.(.+)$ [NC]
  # RewriteRule ^ http://{71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}1{71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}{REQUEST_URI} [L,R=301]

Just uncomment the line 87 and 88.The code should look like this

  # To redirect all users to access the site WITHOUT the ‘www.’ prefix,
  # (http://www.example.com/… will be redirected to http://example.com/…)
  # uncomment the following:
  RewriteCond {71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://{71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}1{71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}{REQUEST_URI} [L,R=301]
Now clear your Browser cache and visit your website (http://yourwebsite.com) it will automatically redirect to www.yourwebsite.com.
Similarly For Doing vice versa that is redirect www.example.com to http://example.com

At line 78 you will find these codes

  # To redirect all users to access the site WITH the ‘www.’ prefix,
  # (http://example.com/… will be redirected to http://www.example.com/…)
  # uncomment the following:
  # RewriteCond {71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}{HTTP_HOST} !^www\. [NC]
  # RewriteRule ^ http://www.{71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}{HTTP_HOST}{71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}{REQUEST_URI} [L,R=301]

Just uncomment the line 81 and 82.The code should look like this

  # To redirect all users to access the site WITH the ‘www.’ prefix,
  # (http://example.com/… will be redirected to http://www.example.com/…)
  # uncomment the following:
  RewriteCond {71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}{HTTP_HOST} !^www\. [NC]
  RewriteRule ^ http://www.{71b550cbed0aca3fea2335d26076176dc834a5ad6e765af844b2cea64fe7483b}{HTTP_HOS

Now clear your Browser cache and visit your website (www.yourwebsite.com) it will automatically redirect to http://yourwebsite.com.