Forum Moderators: phranque
Both of them fail to redirect www requests with an appended port number and/or trailing period after the domain name. That is likely to be an error. They should also be redirected.
RewriteEngine on
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
http://example.com (non-www, and all variants of non-www)
http://www.example.com. (FQDN)
http://www.example.com:80 (port number appended)
http://www.example.com.:80 (both)
http://WwW.eXaMpLe.com (casing)
http://foo.example.com (non-www subdomain)
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com./$1 [R=301,L]
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule ^(.*)$ http%2://www.example.com./$1 [R=301,L]