Forum Moderators: phranque
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# normal canonicalization for non cart cases
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/sc/(billing|thankyou)\.cgi
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# Special case anti-www-canonicalization for cart
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteCond %{REQUEST_URI} ^/cgi-bin/sc/(billing|thankyou)\.cgi
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
http://example.com would match the first two rewrite conditions (yes: host is example.com, yes, path is not the cgi-bin/sc/...) and send an HTTP 301 response back to the browser. http://www.example.com and path /. Likewise, if a search engine finds a link having the "wrong" variant of the domain name for any given path, the 301 will cause the same behavior, and search engines know to update their links such that what they present in results is consistent and as directed. http://www.example.com or http://example.com when used for the special cart pages). In the case of static resources like images, css, javascript, etc. that are used in both contexts, your pages should generate root relative paths, such as /css/styles.css -- the browser will know to use whatever protocol and domain name was used to request the page. These pages are not (or should not, at least) be indexed by search engines, so do not need to be canonicalized -- it's OK if some have the https version and others have the http version of the URL.
# normal http www canonicalization for non-cart cases
# (and excluding common included objects to prevent mixed-content warnings)
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [OR]
RewriteCond %{SERVER_PORT} =443
RewriteCond %{REQUEST_URI} !^/cgi-bin/sc/(billing|thankyou)\.cgi$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|ico|css|js)$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Special case https non-www canonicalization for cart
RewriteCond %{HTTP_HOST} !^(example\.com)?$ [OR]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(cgi-bin/sc/(billing|thankyou)\.cgi)$ https://example.com/$1 [R=301,L]