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

 奋斗中DU_536 发布于 2023-01-19 18:06

我正在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的多种变化和组合

添加$conf['https'] = TRUE;到settings.php

这让我的同事和我疯狂.有人可以帮忙吗?

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]
    

    我会称这是一个至关重要的,因为它很好!

    2023-01-19 18:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有