apache HTTP:.htaccess 中的 X-Forwarded-Proto 在开发环境中导致重定向循环

问题描述 投票:0回答:3

我必须从此更新我的.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弹性负载均衡器后面工作。

这一切似乎在 AWS 上运行良好,但在我的本地环境中我陷入了重定向循环。

如何让此设置在两种环境中都能正常工作?

regex amazon-web-services apache .htaccess mod-rewrite
3个回答
118
投票

要使其在两种环境中工作,您可以结合使用这两种条件:

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

2
投票

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

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

0
投票

@纯粹发展 [NE] No Escape:指示服务器解析输出而不转义字符。

完整的 .htaccess 定义列表: https://htaccessbook.com/free/htaccess-character-definitions.pdf

© www.soinside.com 2019 - 2024. All rights reserved.