4

我正在一个 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.comhttps://www.mysite.com mysite.comhttps://mysite.com%{HTTPS}

我可以直接去https://mysite.com,它工作得很好。该站点的 Apache 访问日志在我期望的位置显示“https://”,我的所有 HTTP 请求也是如此。该站点正在使用负载平衡器(平衡器中只有一个节点)的 RackSpace 服务器上运行。SSL 证书位于 RackSpace 负载平衡器上。我尝试了以下步骤,但没有任何结果:

  • 将 RewriteCond 替换为RewriteCond %{ENV:HTTPS} on [NC]
  • 将 RewriteCond 替换为RewriteCond %{SERVER_PORT} ^443$
  • 上述 RewriteCond 的多种变体和组合
  • 添加$conf['https'] = TRUE;到 settings.php

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

4

1 回答 1

5

阿努巴瓦拯救了这一天!解决方案是按照他的建议使用%{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]

我将把它称为至尊紧缩包装,因为它很好走

于 2014-03-06T16:40:13.873 回答