Skip to content

Instantly share code, notes, and snippets.

@nkeena
Created July 7, 2017 15:04
Show Gist options
  • Save nkeena/9db6cb85284b8b4616c5176854ef6f0f to your computer and use it in GitHub Desktop.
Save nkeena/9db6cb85284b8b4616c5176854ef6f0f to your computer and use it in GitHub Desktop.
Middleware to redirect uppercase URLs to lowercase ones
<?php
namespace App\Http\Middleware;
use Closure;
class RedirectToLowercase
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$path = $request->path();
$pathLowercase = strtolower($path); // convert to lowercase
if ($path !== $pathLowercase) {
// redirect if lower cased path differs from original path
return redirect($pathLowercase);
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment