How to Redirect non-www to www in .htaccess for Joomla site

In order to redirect all of the requests for yourdomain.com to http://www.yourdomain.com, you should set the appropriate rewrite rule. This can be done by adding the following lines at the beginning of the .htaccess file in your WWW folder:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

From now on, when someone accesses http://yourdomain.com they will be redirected to http://www.yourdomain.com.

Using this method is safe because it should not create any redirection loops nor it should interfere with other rewrite rules.

In Joomla, we have to add above rule (code) in .htaccess file, before the joomla .htaccess rule.
So, your .htaccess file look like this….

##  Can be commented out if causes errors, see notes above.
Options +FollowSymLinks
##  mod_rewrite in use
RewriteEngine On
#Custom rule(It must be before the joomla htaccess rule) for non www to www url:Start
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
#Custom rule for non www to www url:End
########## Begin – Rewrite rules to block out some common exploits

Comments