Force users to use http://www.yoursite.com
To force users to use the www version of your domain all you have to do is add the following code to your .htaccess file (just replace yoursite.com with your domain name).
1 2 3 4 | # Redirect non-www urls to www RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.yoursite\.com RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L] |
Alternatively you can use :
1 2 3 4 | # Redirect non-www urls to www RewriteEngine on RewriteCond %{HTTP_HOST} ^example\.com [NC] RewriteRule (.*) http://www.example.com/$1 [R=301,L] |
Force users to use http://yoursite.com
To force users to use the non www version of your domain all you have to do is add the following code to your .htaccess file (just replace yoursite.com with your domain name).
1 2 3 4 | # Redirect www urls to non-www RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC] RewriteRule (.*) http://yoursite.com/$1 [R=301,L] |
Alternatively you can use :
1 2 3 4 | # Redirect www urls to non-www RewriteEngine on RewriteCond %{HTTP_HOST} !^example\.com RewriteRule (.*) http://example.com/$1 [R=301,L] |
Source: http://www.htaccessbasics.com/force-www-nonwww-domain/