Skip to content

Instantly share code, notes, and snippets.

@mladoux
Forked from geggleto/index.php
Last active November 23, 2018 17:40
Show Gist options
  • Save mladoux/ad6d72c94b540cae173316f3c51b77c7 to your computer and use it in GitHub Desktop.
Save mladoux/ad6d72c94b540cae173316f3c51b77c7 to your computer and use it in GitHub Desktop.
Slim 3 Middleware - Force HTTPS
// 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