Apache .htaccess 将 https 重定向到 http,反之亦然

发布于 2025-01-01 00:14:34 字数 660 浏览 4 评论 0原文

我正在尝试安装 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 技术交流群。

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

发布评论

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

评论(1

无远思近则忧 2025-01-08 00:14:34

这看起来是正确的,并且规则对我有用。由于第一条规则有效,我假设 RewriteEngine on 位于这些规则之上。您的 htaccess 文件中是否还有其他规则?可能是其他原因导致了该问题。

我尝试了以下规则,并按照他们的预期进行了重定向。缺少 HTTPS 规则应该不会产生影响。

RewriteEngine on

# Turn SSL on for checkout except for checkout, signup/login and user/login
RewriteCond %{REQUEST_URI} ^(\/checkout)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^(\/signup\/login)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^(\/user\/login)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/ssl/$1 [R=301,L]

# Turn SSL off everything but checkout, signup/login and user/login
RewriteCond %{REQUEST_URI} !(checkout|signup\/login|user\/login) [NC]
RewriteRule ^ssl/(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

我设置这些文件来测试:

  • public/
    • 结帐(内容:结帐)
    • foobar(内容:foobar)
    • 注册/
      • 登录(内容:注册/登录)
    • 用户/
      • 登录(内容:用户/登录)
    • ssl/
      • 结帐(内容:ssl结帐)
      • foobar(内容:ssl foobar)
      • 注册/
        • 登录(内容:ssl注册/登录)
      • 用户/
        • 登录(内容:ssl用户/登录)

您可以尝试这些规则,看看它们是否按预期工作:

$ curl -L http://samhuri.net/checkout
ssl checkout
$ curl -L http://samhuri.net/ssl/checkout
ssl checkout
$ curl -L http://samhuri.net/foobar
foobar
$ curl -L http://samhuri.net/ssl/foobar
foobar

传递 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.

RewriteEngine on

# Turn SSL on for checkout except for checkout, signup/login and user/login
RewriteCond %{REQUEST_URI} ^(\/checkout)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^(\/signup\/login)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^(\/user\/login)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/ssl/$1 [R=301,L]

# Turn SSL off everything but checkout, signup/login and user/login
RewriteCond %{REQUEST_URI} !(checkout|signup\/login|user\/login) [NC]
RewriteRule ^ssl/(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

I setup these files to test with:

  • public/
    • checkout (contents: checkout)
    • foobar (contents: foobar)
    • signup/
      • login (contents: signup/login)
    • user/
      • login (contents: user/login)
    • ssl/
      • checkout (contents: ssl checkout)
      • foobar (contents: ssl foobar)
      • signup/
        • login (contents: ssl signup/login)
      • user/
        • login (contents: ssl user/login)

You can try these rules out and see they work as expected:

$ curl -L http://samhuri.net/checkout
ssl checkout
$ curl -L http://samhuri.net/ssl/checkout
ssl checkout
$ curl -L http://samhuri.net/foobar
foobar
$ curl -L http://samhuri.net/ssl/foobar
foobar

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?

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