-
-
Save mladoux/ad6d72c94b540cae173316f3c51b77c7 to your computer and use it in GitHub Desktop.
Slim 3 Middleware - Force HTTPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Enforce HTTPS | |
$app->add(function (Request $request, Response $response, $next) { | |
$cf = $this->get('settings'); | |
// Check if configuration wants us to enforce https, so we can turn it | |
// on or off depending on whether the server supports https | |
if (isset($cf['force_https']) && $cf['force_https'] === true) { | |
if ($request->getUri()->getScheme() !== 'https') { | |
// Redirect to HTTPS | |
$uri = $request->getUri()->withScheme("https")->withPort(null); | |
return $response->withRedirect( (string)$uri ); | |
} | |
} | |
return $next($request, $response); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment