如何重定向到HTTPS使用的.htaccess在Heroku上雪松堆栈堆栈、雪松、重定向、htaccess

由网友(冷言冷语冷坚强)分享简介:我是新来的云托管... I'm new to cloud hosting...我的工作是托管的在Heroku为雪松的应用程序PHP的Web应用程序。 Heroku上提供了小猪回SSL对所有的子域,这样我就可以加载 https://myapp.herokuapp.com 就好了。但我也可以加载 http://myap...

我是新来的云托管...

I'm new to cloud hosting...

我的工作是托管的在Heroku为雪松的应用程序PHP的Web应用程序。 Heroku上提供了小猪回SSL对所有的子域,这样我就可以加载 https://myapp.herokuapp.com 就好了。但我也可以加载 http://myapp.herokuapp.com 。我想通过重定向 HTTP 要求 HTTPS 强制SSL。

I'm working on a PHP web app that's hosted on Heroku as a "Cedar" app. Heroku offers "piggy back" SSL to all their subdomains, so I can load https://myapp.herokuapp.com just fine. But I can also load http://myapp.herokuapp.com. I want to force SSL by redirecting http requests to https.

通常情况下,这将是容易的。我只想使用mod_rewrite如下:

Normally, this would be easy. I would just use mod_rewrite as follows:

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

但这并不工作在Heroku上!

看来,SSL终止上行,前交通不断打我的应用程序。因此,%{HTTPS} 条件永远都无法满足,其结果是一个重定向循环。我也尝试以下,这也没有工作:

It appears that SSL terminates upstream, before the traffic ever hits my app. So the %{HTTPS} condition is never met, and the result is a redirect loop. I've also tried the following, which also didn't work:

RewriteCond %{SERVER_PORT} != 443 #<--also redirect loop
RewriteCond %{REQUEST_SCHEME} !https #<--also redirect loop

所以我的问题是如何检测/重定向到HTTPS时,它的上游终止?

So my question is how can I detect/redirect-to HTTPS when it's terminated upstream?

推荐答案

在此度过了一整天之后,我想通了!

After spending all day on this, I figured it out!!

这个问题是雄辩地总结here.

The issue is eloquently summarized here.

底线:Heroku的设置它自己的自定义标题来指示交通的原方案(之前SSL终止于负载均衡

Bottom line: Heroku sets it's own custom header to indicate the ORIGINAL scheme of the traffic (before SSL terminated at the load balancer.

所以,这在Heroku .htaccess文件工作原理

So THIS works in an .htaccess file on Heroku

##Force SSL 

#Normal way (in case you need to deploy to NON-heroku)
RewriteCond %{HTTPS} !=on

#Heroku way
RewriteCond %{HTTP:X-Forwarded-Proto} !https 

#If neither above conditions are met, redirect to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

秘决与行 HTTP:X-转发,原

希望这可以帮助别人!在写这个的时候有这个ZERO文档。

Hope this helps someone else! At the time of writing this there is ZERO documentation on this.

阅读全文

相关推荐

最新文章