getMethod() === 'GET' && $this->responseFactory) { $uri = $request->getUri(); $path = $this->normalize($uri->getPath()); if ($uri->getPath() !== $path) { return $this->responseFactory->createResponse(301) ->withHeader('Location', (string) $uri->withPath($path)); } } return $handler->handle($request); } private function normalize(string $path): string { if ($path === '') { return '/'; } if (strlen($path) > 1) { return rtrim($path, '/'); } return $path; } }