我们的代码在两个不同的服务器上运行。一个启用了 https,而另一个(“旧版”)则没有。我们想强制使用 https,但仅限于 SSL 服务器。
我添加了以下代码,但它导致了重定向循环。
#Force SSL for everyone but legacy production
RewriteCond %{HTTP_HOST} !legacy\.site\.com
RewriteCond %{HTTPS} !=on
#RewriteCond %{SERVER_PORT} !=443 #<--this also causes redirect loop
#RewriteCond %{REQUEST_SCHEME} != https #<--this causes a 500 error!
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
以下是以下内容(我不认为这很重要,但为了完整性我将其包括在内):
#gzip
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
RewriteEngine on
RewriteCond $1 !^(index\.php|public|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
为什么?
更新 服务器是一个 Heroku Cedar 应用程序,它使用“搭载”SSL:https://blog.heroku.com/archives/2012/5/3/announcing_better_ssl_for_your_app
最佳答案
非常感谢@anubhava 和@arco444 深思熟虑的评论...
事实证明,我遇到的重定向循环是由于 SSL 在 Heroku Cedar 堆栈应用程序的上游终止而导致的。这意味着流量的最后一段永远不会是 https,因此常规技术将永远重定向 = 重定向循环。
解决此问题的方法(在 Heroku 上)是为其特殊的自定义 header X-Forwarded-Proto
添加一个 RewriteCond。所以这有效:
##Force SSL for everyone but legacy production
RewriteCond %{HTTP_HOST} !legacy\.site\.com
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
关于.htaccess 重定向循环由 http --> https 重写规则产生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26487034/