Skip to content

Instantly share code, notes, and snippets.

@stevebauman
Created March 28, 2019 19:58
Show Gist options
  • Save stevebauman/97a49f7dd728a69e8dc0942981caeb1f to your computer and use it in GitHub Desktop.
Save stevebauman/97a49f7dd728a69e8dc0942981caeb1f to your computer and use it in GitHub Desktop.
A Laravel HTTP middleware to force HTTPS requests. Doesn't run when running in local development or when running unit tests.
<?php
namespace App\Http\Middleware;
use Closure;
class ForceHttps
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
// If the application is running in production and the request is
// not secure, we'll redirect users to the secure URL.
if (
! app()->isLocal() &&
! app()->runningUnitTests() &&
! $request->isSecure()
) {
return redirect()->secure($request->path());
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment