diff --git a/CHANGELOG.md b/CHANGELOG.md index 45c5d35..62e0c37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Laravel URL Route Trailing Slash ================================ +1.1.12 Under Development +------------------------ + +- Bug #15: Fixed `RedirectTrailingSlash` middleware no longer allows multiple trailing slashes (klimov-paul) + + 1.1.11, March 25, 2024 ---------------------- diff --git a/src/Middleware/RedirectTrailingSlash.php b/src/Middleware/RedirectTrailingSlash.php index 0a84401..26866f6 100644 --- a/src/Middleware/RedirectTrailingSlash.php +++ b/src/Middleware/RedirectTrailingSlash.php @@ -68,12 +68,12 @@ public function __construct(Container $container) */ public function handle($request, Closure $next) { - if (! in_array($request->getMethod(), ['GET', 'HEAD', 'OPTIONS'])) { + if (!in_array($request->getMethod(), ['GET', 'HEAD', 'OPTIONS'])) { return $next($request); } $currentRoute = $request->route(); - if (! $currentRoute instanceof Route) { + if (!$currentRoute instanceof Route) { return $next($request); } @@ -86,16 +86,24 @@ public function handle($request, Closure $next) } if (Str::endsWith($pathInfo, '/')) { - if (! $currentRoute->hasTrailingSlash) { + if ($currentRoute->hasTrailingSlash) { + $expectedPathInfo = rtrim($pathInfo, '/') . '/'; + if ($expectedPathInfo !== $pathInfo) { // multiple trailing slashes, e.g. '/path/info///' + $url = $this->createRedirectUrl($request, $expectedPathInfo); + + return $this->redirect($url); + } + } else { $url = $this->createRedirectUrl($request, rtrim($pathInfo, '/')); return $this->redirect($url); } + return $next($request); } if ($currentRoute->hasTrailingSlash) { - $url = $this->createRedirectUrl($request, $pathInfo.'/'); + $url = $this->createRedirectUrl($request, $pathInfo . '/'); return $this->redirect($url); } @@ -120,7 +128,7 @@ protected function createRedirectUrl($request, $newPath): string $url .= $newPath; if (($queryString = $request->getQueryString()) !== null) { - $url .= '?'.$queryString; + $url .= '?' . $queryString; } return $url; diff --git a/tests/Middleware/RedirectTrailingSlashTest.php b/tests/Middleware/RedirectTrailingSlashTest.php index 84cb33a..a03ee5b 100644 --- a/tests/Middleware/RedirectTrailingSlashTest.php +++ b/tests/Middleware/RedirectTrailingSlashTest.php @@ -101,6 +101,11 @@ public static function dataProviderRedirect(): array 'http://example.com/foo/bar/?name=value', 'http://example.com/foo/bar?name=value', ], + [ + 'foo/bar/', + 'http://example.com/foo/bar//', + 'http://example.com/foo/bar/' + ], ]; }