我需要将网站的所有请求重定向到 www,仅将某些页面重定向到不带 www 的 https,因为由于某种原因,我们的客户购买的 ssl 证书不涵盖 www。 环顾四周后,我设法完成了大部分工作。但是,一旦我们访问了安全 url 页面,其他页面将保留在 https 上。 这是我到目前为止在 .htaccess 中的内容:
#Redirect to www
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} ^[^./]+\.[^./]+$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^./]+)\.[^./]+\.[^./]+$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Force SSL on checkout login account and admin pages
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} checkout|login|my-account|administrator|webshop
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L
如果 https 已打开并且不属于列出的网址,我会错过将重定向到非安全 www 的部分。但我对正则表达式和重写规则一点也不熟悉。 一些帮助将不胜感激。
最佳答案
尝试使用此代码:
# Force SSL on checkout login account and admin pages
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} checkout|login|my-account|administrator|webshop
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ https://%2/$1 [R=301,L,QSA]
# Remove SSL on other pages
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !checkout|login|my-account|administrator|webshop
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%2/$1 [R=301,L,QSA]
# Force www for non https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,QSA]
关于.htaccess - htaccess 仅针对某些页面重定向到 ssl,而 www 对于其他页面不安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12260816/