I need to force all routes with SSL in Lumen 5.6. For examples http://www.mywebsite.com to https://www.mywebsite.com
I tried many different solutions on the web, but nothing worked for me: Middelware, .htaccess, AppServiceProvider
Which the best way to force SSL scheme in Lumen 5.6?
You can create a middleware class and use the redirect()->to
function with the secure
parameter set to true
.
To achieve this, create a class (HttpsProtocol.php
) and place it in the middleware directory:
namespace App\Http\Middleware;
use Closure;
class HttpsProtocol{
/**
* @param \Illuminate\Http\Request $request
* @param Closure $next
*
* @return \Illuminate\Http\Response|\Laravel\Lumen\Http\ResponseFactory|mixed
*/
public function handle($request, Closure $next) {
if (!$request->secure() && app()->environment() === 'production') {
return redirect()->to($request->getRequestUri(), 302, [], true);
}
return $next($request);
}
}
And add the middleware to your $app->middleware
array found in app.php
.
$app->middleware([
App\Http\Middleware\HttpsProtocol::class
]);