仅使用 .htaccess 在某些页面上将 http 重写为 https
我知道关于我的问题有很多话题。我检查了所有这些并尝试了它们,但无法使其工作。 我只需要在某些页面上将 http 重写为 https 。访问 https 页面后,URL 将返回到 http。 这就是我到目前为止所拥有的:
# Rewrite Rules for domain.com
RewriteEngine On
RewriteBase /
#Rewrite www to domain.com
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
#Rewrite to https
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /secure.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
#traffic to http://, except secure.php
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /secure.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
当我跳过最后一组规则(流量到 http://,secure.php 除外)时,secure.php 页面加载为 https 并被加密。美好的。使用最后一组规则(流量为 http://,secure.php 除外),URL 重写为 https,变为蓝色 (SSL) 一秒钟,然后消失(无 SSL),但 URL 仍然是 https。
有什么想法吗? 雅采克
I know there is lots of topics regarding my question. I checked them all and tried them out but can't make it work.
I need to rewrite http to https on some pages only. After visiting https pages the URL would go back to http.
This is what I have so far:
# Rewrite Rules for domain.com
RewriteEngine On
RewriteBase /
#Rewrite www to domain.com
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
#Rewrite to https
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /secure.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
#traffic to http://, except secure.php
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /secure.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
When I skip the last set of rules (traffic to http://, except secure.php) the secure.php page loads as https and is encrypted. FINE. With the last set of rules (traffic to http://, except secure.php) the URL rewrites to https, turns blue (SSL) for a second and disappears (no SSL) but the URL is still https.
Any idea?
Jacek
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第三组规则有错误。
解决方案如下:使用第三组规则:如果 https 为“on”,则如果 URL 不包含“/secure”,则重定向到 http。
题外话建议:请查找 Yahoo Slow 以及他们关于网站优化的非常聪明的建议,您就会明白为什么前面加上“www”总是更好。您最好对#1 重写规则执行相反的操作:如果没有“www”,则添加它。
请尝试使用
RewriteLog
指令:它可以帮助您追踪此类问题:告诉我它是否有效。
There's a mistake with the 3rd set of rules.
Here's the solution: with the 3rd set of rules: if the https is "on" then if the URL doesn't contain "/secure" then redirect to http.
Off topic advice: please look for Yahoo Slow an their very clever advices on website optimisation and you'll see why it's always better to have the "www" before. You'd better do the opposite for your #1 rewrite rule: if there's no "www", then add it.
Please try to use the
RewriteLog
directive: it helps you to track down such problems:Tell me if it works.