bug #29373 [Routing] fix trailing slash redirection (nicolas-grekas)
This PR was merged into the 4.1 branch.
Discussion
----------
[Routing] fix trailing slash redirection
| Q | A
| ------------- | ---
| Branch? | 4.1
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #29363
| License | MIT
| Doc PR | -
Commits
-------
fbaba23023
[Routing] fix trailing slash redirection
This commit is contained in:
commit
bce7748a37
@ -550,9 +550,15 @@ EOF;
|
||||
private function compileSwitchDefault(bool $hasVars, bool $matchHost): string
|
||||
{
|
||||
$code = sprintf("
|
||||
if ('/' !== \$pathinfo && \$hasTrailingSlash !== ('/' === \$pathinfo[-1])) {
|
||||
if ('/' !== \$pathinfo) {
|
||||
if (!\$hasTrailingSlash && '/' === \$pathinfo[-1]%s) {
|
||||
%s;
|
||||
}
|
||||
if (\$hasTrailingSlash && '/' !== \$pathinfo[-1]) {
|
||||
%2\$s;
|
||||
}
|
||||
}\n",
|
||||
$hasVars ? ' && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n[\'MARK\']' : '',
|
||||
$this->supportsRedirections ? 'return null' : 'break'
|
||||
);
|
||||
|
||||
|
@ -160,9 +160,14 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($supportsTrailingSlash && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ($supportsTrailingSlash) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1))) {
|
||||
return;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$hostMatches = array();
|
||||
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
|
||||
|
@ -54,9 +54,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
}
|
||||
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($requiredHost) {
|
||||
if ('#' !== $requiredHost[0] ? $requiredHost !== $host : !preg_match($requiredHost, $host, $hostMatches)) {
|
||||
@ -232,9 +237,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
|
||||
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
|
||||
break;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $i => $v) {
|
||||
if (isset($matches[1 + $i])) {
|
||||
|
@ -2793,9 +2793,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
|
||||
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
|
||||
break;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $i => $v) {
|
||||
if (isset($matches[1 + $i])) {
|
||||
|
@ -118,9 +118,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
||||
|
||||
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
|
||||
return null;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $i => $v) {
|
||||
if (isset($matches[1 + $i])) {
|
||||
|
@ -63,9 +63,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
|
||||
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
|
||||
break;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $i => $v) {
|
||||
if (isset($matches[1 + $i])) {
|
||||
|
@ -91,9 +91,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
||||
}
|
||||
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1]) {
|
||||
return null;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if ($requiredHost) {
|
||||
if ('#' !== $requiredHost[0] ? $requiredHost !== $host : !preg_match($requiredHost, $host, $hostMatches)) {
|
||||
@ -269,9 +274,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
||||
|
||||
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
|
||||
return null;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $i => $v) {
|
||||
if (isset($matches[1 + $i])) {
|
||||
|
@ -46,9 +46,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
}
|
||||
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
|
||||
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
|
||||
@ -82,9 +87,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
|
||||
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
|
||||
break;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $i => $v) {
|
||||
if (isset($matches[1 + $i])) {
|
||||
|
@ -66,9 +66,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
}
|
||||
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
|
||||
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
|
||||
|
@ -83,9 +83,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
||||
}
|
||||
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1]) {
|
||||
return null;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
|
||||
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
|
||||
@ -121,9 +126,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
||||
|
||||
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
|
||||
return null;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $i => $v) {
|
||||
if (isset($matches[1 + $i])) {
|
||||
|
@ -44,9 +44,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
}
|
||||
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
|
||||
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
|
||||
@ -98,9 +103,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
|
||||
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
|
||||
break;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $i => $v) {
|
||||
if (isset($matches[1 + $i])) {
|
||||
|
@ -79,9 +79,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
||||
}
|
||||
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1]) {
|
||||
return null;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
|
||||
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
|
||||
@ -133,9 +138,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
||||
|
||||
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
|
||||
return null;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $i => $v) {
|
||||
if (isset($matches[1 + $i])) {
|
||||
|
@ -51,9 +51,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
|
||||
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
|
||||
|
||||
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
|
||||
if ('/' !== $pathinfo) {
|
||||
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
|
||||
break;
|
||||
}
|
||||
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $i => $v) {
|
||||
if (isset($matches[1 + $i])) {
|
||||
|
@ -680,6 +680,15 @@ class UrlMatcherTest extends TestCase
|
||||
$this->assertEquals('b', $matcher->match('/bar/abc.123')['_route']);
|
||||
}
|
||||
|
||||
public function testSlashVariant()
|
||||
{
|
||||
$coll = new RouteCollection();
|
||||
$coll->add('a', new Route('/foo/{bar}', array(), array('bar' => '.*')));
|
||||
|
||||
$matcher = $this->getUrlMatcher($coll);
|
||||
$this->assertEquals('a', $matcher->match('/foo/')['_route']);
|
||||
}
|
||||
|
||||
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
|
||||
{
|
||||
return new UrlMatcher($routes, $context ?: new RequestContext());
|
||||
|
Reference in New Issue
Block a user