getUri(); $path = $uri->getPath(); if ('/' != $path && '/' == substr($path, -1)) { // recursively remove slashes when its more than 1 slash $path = rtrim($path, '/'); // permanently redirect paths with a trailing slash // to their non-trailing counterpart $uri = $uri->withPath($path); if ('GET' == $request->getMethod()) { $response = new Response(); return $response ->withHeader('Location', (string) $uri) ->withStatus(301); } else { $request = $request->withUri($uri); } } return $handler->handle($request); } }