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

gre*_*reg 46 apache .htaccess mod-rewrite redirect amazon-web-services

我不得不更新我的.htaccess:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)

对此:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)

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

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

如何让这个设置在两种环境中正常工作?

anu*_*ava 88

要使它在两种环境中都可以使用,您可以结合以下两种条件

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Run Code Online (Sandbox Code Playgroud)

  • 这实际上没有[OR],但是它会导致重定向循环. (2认同)
  • 如果您使用loadbalancer,请确保获得X-Forwarded-Proto (2认同)