下面代码应该写在RewriteBase / 之后
强制https:
RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
强制http:
RewriteCond %{HTTPS} on RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
无www转向至www:
# Redirect non-www to www RewriteCond %{HTTP_HOST} !^www\.(.*) RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
有www转向至无:
# Redirect www to non-www RewriteCond %{HTTP_HOST} ^www\.(.*) RewriteRule (.*) http://%1/$1 [R=301,L]