仅使用 .htaccess 在某些页面上将 http 重写为 https

发布于 2024-08-24 03:53:54 字数 825 浏览 1 评论 0原文

我知道关于我的问题有很多话题。我检查了所有这些并尝试了它们,但无法使其工作。 我只需要在某些页面上将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月棠 2024-08-31 03:53:54

第三组规则有错误。
解决方案如下:使用第三组规则:如果 https 为“on”,则如果 URL 不包含“/secure”,则重定向到 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} on
RewriteCond %{REQUEST_URI} !(/secure.php)
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]

题外话建议:请查找 Yahoo Slow 以及他们关于网站优化的非常聪明的建议,您就会明白为什么前面加上“www”总是更好。您最好对#1 重写规则执行相反的操作:如果没有“www”,则添加它。


请尝试使用RewriteLog指令:它可以帮助您追踪此类问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

告诉我它是否有效。

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.

# 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} on
RewriteCond %{REQUEST_URI} !(/secure.php)
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]

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:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

Tell me if it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文