Developing SEO friendly website, you have to decide the domain name is whether it is with "WWW" or without "WWW". as per your need you can transform the user behavior by redirecting reversal order based on your need. If you decide without WWW mean, while user attempt to type with http://www.example.com you need to redirect http://example.com, If you decide with WWW mean, you need to redirect with www when user typing http://example.com to http://www.example.com
In this case, We have the great option in .htaccess to decide with or without WWW .
Here is the solutions for htaccess redirect with or without www
Examples with mentioning Domain Name
Redirect WWW to non-WWW or without WWW Domain Name:
Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Redirect non-WWW to with WWW Domain Name:
Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com [NC] Rewriterule ^(.*)$ http://www.example.com/$1 [R=301,NC]
Example with Suitable for any Website without mentioning the domain name:
(Suitable for any Website) no need to tell the domain name on code.
Redirect www to non-www (both: http + https)
if we need to do this for separate http and https:
Options +FollowSymLinks RewriteEngine On # if without https RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] # if https RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Redirect non-www to www (both: http + https)
if we need to do this for separate http and https:
Options +FollowSymLinks RewriteEngine On # if without https RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] # if https RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]