【问题标题】:Why isn't RewriteCond %{HTTPS} returning the correct value?为什么 RewriteCond %{HTTPS} 没有返回正确的值?
【发布时间】:2014-04-08 08:12:26
【问题描述】:

我正在开发一个 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$
  • 上述 RewriteCond 的多种变体和组合
  • 在 settings.php 中添加了$conf['https'] = TRUE;

这让我和我的同事都快疯了。有人可以帮忙吗?

【问题讨论】:

  • 如果您使用代理,请尝试:%{HTTP:X-Forwarded-Proto} 变量。
  • 成功了!非常感谢您的好先生。
  • 不客气,很高兴它成功了。

标签: .htaccess mod-rewrite drupal https no-www


【解决方案1】:

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

# 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]

我将把它称为至高无上的紧缩包装,因为它是很好

【讨论】:

  • 非常感谢,这完全拯救了我的一天 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-13
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多