laravel 强制跳转 https 解决办法

版权声明: https://blog.csdn.net/qq_29785317/article/details/82706802

路由要从http redirect 到 https,可以改 nginx/apache 配置。如果不想在web server中做这些修改配置,可以尝试在laravel框架中解决

在网上里看到的一个方案是 写一个 全局中间件,将所有的请求转换成 https 【利用$request->scure() 来判断 和 转化】
不过我发现这个做法在直接访问首页的时候并不能实现转化 (route为 ” / ” 的时候)

然后换了方法:

1.在 app/Providers/AppServiceProvider.php 的 boot()方法 中 添加
  URL::forceScheme('https');
2.在路由文件web.php中 添加

/****  以下两次跳转是为了实现所有请求跳转到https  start******/
Route::get('/', function () {
    return redirect('/login');
});

Route::get('/login', function(){
    return redirect('/static/#/login');
});
/****  两次跳转是为了实现所有请求跳转到https end ******/

关键是将对首页的访问 重定向到一个新的laravel路由,这样它就会走 forceScheme 的流程,之后的所有请求都是https了

solved~!

猜你喜欢

转载自blog.csdn.net/qq_29785317/article/details/82706802