Skip to content

Commit

Permalink
Fixed RedirectTrailingSlash middleware no longer allows multiple tr…
Browse files Browse the repository at this point in the history
…ailing slashes
  • Loading branch information
klimov-paul committed May 17, 2024
1 parent a0052d0 commit 1f398a2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
----------------------

Expand Down
18 changes: 13 additions & 5 deletions src/Middleware/RedirectTrailingSlash.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand All @@ -120,7 +128,7 @@ protected function createRedirectUrl($request, $newPath): string
$url .= $newPath;

if (($queryString = $request->getQueryString()) !== null) {
$url .= '?'.$queryString;
$url .= '?' . $queryString;
}

return $url;
Expand Down
5 changes: 5 additions & 0 deletions tests/Middleware/RedirectTrailingSlashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
],
];
}

Expand Down

0 comments on commit 1f398a2

Please sign in to comment.