Forum Moderators: phranque
That *is* a standard non-www to www redirect (though you do need to remember to escape the period in the pattern).
I'm not sure what you mean by "the period in the pattern".
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/ [L,R=301]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteEngine On directive exactly once.
The first ruleset redirects ALL non-www URLs to the root of www.
The second ruleset redirects ALL non-www URLs to the www and preserves the page name in the request.
The second ruleset will never run, because the first ruleset matches all non-www requests.
The second ruleset is missing the escaping.
The first ruleset is redundant.
You also need the RewriteEngine On directive exactly once.
unless the \ (escape) will make the difference.
RewriteEngine on
#
# Redirect requests for non-blank non-canonical non-www hostname to same URL-path on canonical host
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www\.example\.com/$1 [R=301,L]
RewriteEngine on
#
# Redirect requests for non-blank non-canonical non-www hostname to same
# URL-path on canonical host, preserving requested http/https protocol
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]