PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites

27 Nov 2013

How to redirect to www using .htaccess

                       Type your domain name without www in address bar. Check whether it redirects to your site or give a 301 error message. If you got 301 error message, then you will solve this problem using .htaccess.

Syntax:
              RewriteEngine On
              RewriteCond %(HTTP_HOST) ! ^www.domainname.com$ [NC]
              RewriteRule ^(.*)$ http://www.domainname.com/$1 [L, R=301]

Explanation:
              Where,
                          .htaccess force the all http requests to use either www.domainname.com or domainname.com .
                           HTTP_HOST - represents your domain name.
                           RewriteCond   - check the conditions if you typed domain name is not start with www, it 
redirect to next rewrite rule. 
                           ! ^www.domainname.com$ - means you typed domain name is not start with www.
                           NC - represents not case sensitive.
                           RewriteRule - means it redirect domain name without www to http://www.doaminname.com.
                           ^ - starting
                           .* - one or more characters.
                           $ - ending
                           L - if rule matches, don't process any other rewrite rules below this one.