Apache .htaccess 将 https 重定向到 http,反之亦然
我正在尝试安装 drupal 7 以将三个页面重定向到 https 并强制其他所有页面返回到 http。 单独来看,这里的第一个重写规则有效。启用两者后,没有任何效果,并且全部重定向回 /index.php
# Turn SSL on for checkout except for checkout, signup/login and user/login
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^(\/checkout)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^(\/signup\/login)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^(\/user\/login)$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Turn SSL off everything but checkout, signup/login and user/login
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(checkout|signup\/login|user\/login) [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
I'm trying to get a drupal 7 install to redirect three pages to https and force everything else back to http.
In isolation, the first rewrite rule here works. With both enabled, nothing works and it all redirects back to /index.php
# Turn SSL on for checkout except for checkout, signup/login and user/login
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^(\/checkout)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^(\/signup\/login)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^(\/user\/login)$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Turn SSL off everything but checkout, signup/login and user/login
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(checkout|signup\/login|user\/login) [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来是正确的,并且规则对我有用。由于第一条规则有效,我假设
RewriteEngine on
位于这些规则之上。您的 htaccess 文件中是否还有其他规则?可能是其他原因导致了该问题。我尝试了以下规则,并按照他们的预期进行了重定向。缺少 HTTPS 规则应该不会产生影响。
我设置这些文件来测试:
您可以尝试这些规则,看看它们是否按预期工作:
传递
curl
-i
标志以查看标头和状态代码。所以我怀疑还有其他问题。您还有其他规则来重定向到 https 或从 https 重定向吗?
This looks correct and the rules work for me. Since the first rule works I'll assume
RewriteEngine on
is somewhere above these rules. Are there other rules in your htaccess files? Could be something else causing the issue.I tried the following rules and am redirected as expected by both of them. The absence of the HTTPS rule shouldn't make a difference.
I setup these files to test with:
You can try these rules out and see they work as expected:
Pass
curl
the-i
flag to see headers and status codes.So I suspect something else is awry. Do you have any other rules to redirect to or from https?