gpt4 book ai didi

.htaccess - 为什么 RewriteCond %{HTTPS} 没有返回正确的值?

转载 作者:行者123 更新时间:2023-12-05 01:04:55 25 4
gpt4 key购买 nike

我正在一个 Drupal 站点上工作,客户要求我们删除“www”。从网址。这非常简单,我以前做过;我只是在 Drupal 生成的 .htaccess 文件中注释掉建议的行,如下所示:

# To redirect all users to access the site WITHOUT the 'www.' prefix,
# (http://www.example.com/... will be redirected to http://example.com/...)
# uncomment the following:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

熟悉 Drupal 的 .htaccess 的人都知道环境变量 protossl设置在文件的顶部,如下所示:
# Set "protossl" to "s" if we were accessed via https://.  This is used later
# if you enable "www." stripping or enforcement, in order to ensure that
# you don't bounce between http and https.
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]

这在我的本地环境中运行良好,但是当我将更改部署到生产站点时它会中断。 www.mysite.com重定向到 mysite.com正如预期的那样,但是 https://www.mysite.com 还有重定向到 mysite.com而不是 https://mysite.com .似乎 %{HTTPS}变量返回“关闭”,即使它应该是“打开”。

我可以直接去 https://mysite.com它工作得很好。该站点的 Apache 访问日志在我期望的位置显示“https://”,我的所有 HTTP 请求也是如此。该站点正在使用负载平衡器(平衡器中只有一个节点)的 RackSpace 服务器上运行。 SSL 证书位于 RackSpace 负载平衡器上。我尝试了以下步骤,但没有任何结果:
  • 将 RewriteCond 替换为 RewriteCond %{ENV:HTTPS} on [NC]
  • 将 RewriteCond 替换为 RewriteCond %{SERVER_PORT} ^443$
  • 以上 R​​ewriteCond 的
  • 的多种变体和组合
  • 已添加 $conf['https'] = TRUE;到 settings.php

  • 这让我的同事和我发疯了。任何人都可以帮忙吗?

    最佳答案

    anubhava拯救了一天!解决方案是使用 %{HTTP:X-Forwarded-Proto}变量正如他所建议的那样。我将 .htaccess 的协议(protocol)检测位更新为如下所示:

    # Set "protossl" to "s" if we were accessed via https://.  This is used later
    # if you enable "www." stripping or enforcement, in order to ensure that
    # you don't bounce between http and https.
    RewriteRule ^ - [E=protossl]
    # The default proto detection provided by Drupal does not work on our
    # production server because it sits behind a load-balancing server.
    # This additional RewriteCond makes sure we can detect the forwarded proto
    RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
    RewriteCond %{HTTPS} on
    RewriteRule ^ - [E=protossl:s]

    我会称这是一个至高无上的紧缩包装,因为它很好去!

    关于.htaccess - 为什么 RewriteCond %{HTTPS} 没有返回正确的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211831/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com