.htaccess - htaccess 仅针对某些页面重定向到 ssl,而 www 对于其他页面不安全

标签 .htaccess mod-rewrite redirect ssl

我需要将网站的所有请求重定向到 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/

相关文章:

php - 子域重定向到完整 URL 并保留原始子域

apache - htaccess 和 htpasswd 问题

css - 重写 URL 后缺少 CSS 和图像

.htaccess 子域重定向不起作用

python - html View 中缺少 django 模型下拉列表

javascript - 如何使用 .htaccess 阻止 ip

html - htaccess 在 CHROME 中重写两次

.htaccess - 简单的 RewriteRule 模式不起作用

iphone - 智能手机和 Googlebot-mobile 的重定向

tomcat - 本地主机转发本地主机 :8080/blah in tomcat6