How To Force Redirect HTTP to HTTPS in Laravel ?

Admin   Laravel   232  2021-03-16 04:50:05

Hi Guys,

Today, I will teach you how to force redirect http to https in laravel application.We can redirect using .htaccess, Nginx config etc.

When i was working on my first laravel application and i make it live and i want secure connection using ssl, i buy from hosting provider. But i was thinking how to redirect http to https of url in Laravel.

Create Middleware

You can create middleware then you can use bellow command to create middleware in laravel.So Let's Open terminal and run bellow command:

 

php artisan make:middleware redirectSecureHttp

Run successfully above command after just copy and paste bellow code.

app\Http\Middleware\redirectSecureHttp.php

 

public function handle($request, Closure $next){

if (!$request->secure()) {

return redirect()->secure($request->path());

}

return $next($request);

}

We have to need add middleware into kernel file So Let's open kernel.php file add middleware.

app\Http\kernel.php

 

protected $middleware = [

....

\App\Http\Middleware\redirectSecureHttp::class,

....

];

It will help you...