php - Redirect https with laravel
I build a website and i want redirect http (local) to https (use online). I tried using middleware, using AppServiceProvider
, .htacess
, RouteServiceProvider
but it didn't work.
1, When i use middleware
class HttpsProtocol
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (!app()->environment('local')) {
// for Proxies
Request::setTrustedProxies([$request->getClientIp()],
Request::HEADER_X_FORWARDED_ALL);
if (!$request->isSecure()) {
return redirect()->secure($request->path(), 301);
}
}
return $next($request);
}
}
Answer
Solution:
Just open AppServiceProvider and add
\URL::forceScheme('https');
in the boot function. thats all.
public function boot()
{
\URL::forceScheme('https');
}
Source