在Heroku Laravel实例上设置HTTPS重定向 [英] Setting up HTTPS redirects on Heroku Laravel Instance

查看:117
本文介绍了在Heroku Laravel实例上设置HTTPS重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行在apache Heroku实例上的Laravel 5构建,我试图确保所有流量都通过https转换,但是我拼命地输了。



我已经启动并成功运行SSL证书。但是,使用Heroku,您无法直接在其服务器上编辑.htaccess文件。所以他们对这个页面的建议是建立一个apache_app.conf并告诉Heroku通过将它放在Procfile中来读取它:

  web:vendor / bin / heroku-php-apache2 -C apache_app.conf public / 

然而,当我这样做时,几乎所有的HTTPS重写规则都会导致重定向循环或应用程序错误。这是我目前的配置:

pre $ $ $ c $ rewriteEngine
$ b#正常的方式(如果你需要部署to NON-heroku)
RewriteCond%{HTTPS}!= on

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

#如果上述条件都不符合,重定向到https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R = 301]


解决方案

不是通过.htaccess处理重定向,而是通过方法



HttpsProtocol中间件会将每一个网络路由重定向到HTTPS协议

I have a Laravel 5 build running on an apache Heroku instance and I am trying to make sure all traffic is converted through https, however I am desperately lost.

I have the SSL certificate up and running successfully. However with Heroku you cannot edit the .htaccess file on their server directly. So their recommendation on this page is to set up an apache_app.conf and tell Heroku to read it by putting this in the Procfile:

web: vendor/bin/heroku-php-apache2 -C apache_app.conf public/

However when I do this, pretty much any kind of HTTPS rewrite rules I add cause a redirect loop or an application error. Here is my current configuration:

RewriteEngine on

#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]

解决方案

Instead of handling redirect by .htaccess I was able to do it by this method

HttpsProtocol middleware will redirect every web route to the HTTPS protocol

这篇关于在Heroku Laravel实例上设置HTTPS重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆