.htaccess 重定向循环由 http --> https 重写规则产生

标签 .htaccess mod-rewrite redirect ssl heroku

我们的代码在两个不同的服务器上运行。一个启用了 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/

相关文章:

redirect - Laravel 重定向会影响跳出率吗?

apache - .htaccess 重写到子文件夹

php - WordPress https 重定向循环

.htaccess - 410 状态代码所需的 mod_rewrite 帮助

php - mod_rewrite 和 apache clean 超链接

apache - 是否可以将我网站的特定页面(使用 https)重定向到 http?

PHP:页面未重定向回登录页面

.htaccess - modrewrite,变量中的符号

php - htaccess 将所有重定向到 https 到 http 除了一个文件夹

php - 如何在 Laravel 中做一个简单的重定向?