下面代码应该写在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]

 

作者 听涛

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注