Monday, September 11, 2017

Redirect http to https in laravel

Tags



Redirect http to https in laravel is easy, all you have to do is just create a
middleware and attach it to your route.

1. Create a middleware.

namespace MyApp\Http\Middleware;

use Closure;

class HttpsProtocol {

   public function handle($request, Closure $next)
   {
           if (!$request->secure() && env('APP_ENV') === 'prod') {
               return redirect()->secure($request->getRequestUri());
           }

           return $next($request);
   }
}

2. Register your middleware in kernel.php

'https_protocol' => \App\Http\Middleware\MiddlewareClassName::class
Note that the namespace path is depends where you store your middleware.
Change it according to where you save your middleware

3. Attach your middleware to your route.

Route::get('profile', 'UserController@show')->middleware('https_protocol');

4. Done. :)

You should be redirecting to https the moment you accessing to http.

Thanks for watching my blog. Do follow me on google+ or like my facebook page for more! :)


EmoticonEmoticon