【问题标题】:apache HTTP:X-Forwarded-Proto in .htaccess is causing redirect loop in dev environment.htaccess 中的 apache HTTP:X-Forwarded-Proto 导致开发环境中的重定向循环
【发布时间】:2014-12-24 14:03:45
【问题描述】:

我不得不从这里更新我的 .htaccess:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

到这里:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

让它在 AWS Elasic 负载均衡器后面工作。

在 AWS 上似乎一切正常,但在我的本地环境中,我陷入了重定向循环。

我怎样才能让这个设置在两种环境中都能正常工作?

【问题讨论】:

  • 您是否将本地 Apache 服务器作为反向代理/负载平衡器运行?您是否在本地 VirtualHost 配置中设置了 RequestHeader set X-Forwarded-Proto "https"

标签: regex amazon-web-services apache .htaccess mod-rewrite


【解决方案1】:

要使其在两种环境中都能正常工作,您可以结合两种条件:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

【讨论】:

  • 这实际上可以在没有 [OR] 的情况下工作,但它会导致重定向循环。
  • 这适用于我的主页http://www.ex.com redirects to https,但如果有人转到直接页面``ex.com/about` 不会重定向到 https。我支持 AWS 负载均衡器
  • 你是赢家兄弟!!
  • 只要确保,如果你使用负载均衡器,你会得到 X-Forwarded-Proto
  • 请问在这种情况下,NE(noescape)标志是什么?
【解决方案2】:

我有一个共享主机(masterhost)的特殊情况,一切都失败了,除了:

  RewriteCond %{HTTP:X-Forwarded-Port} !443
  RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2019-09-16
    相关资源
    最近更新 更多