From 3e8b6a2cb8c2956439b16053e09877509572e01f Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Wed, 11 Nov 2026 17:05:52 +0700 Subject: [PATCH 1/4] fix(Router): handle {locale} placeholder in redirect routes --- system/Router/Router.php | 33 +++++++++++++++++------------- tests/system/Router/RouterTest.php | 3 +++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/system/Router/Router.php b/system/Router/Router.php index f0f9555ad1ac..02426fcc9106 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -457,20 +457,6 @@ protected function checkRoutes(string $uri): bool // Does the RegEx match? if (preg_match('#^' . $routeKey . '$#u', $uri, $matches)) { - // Is this route supposed to redirect to another? - if ($this->collection->isRedirect($routeKey)) { - // replacing matched route groups with references: post/([0-9]+) -> post/$1 - $redirectTo = preg_replace_callback('/(\([^\(]+\))/', static function (): string { - static $i = 1; - - return '$' . $i++; - }, is_array($handler) ? key($handler) : $handler); - - throw new RedirectException( - preg_replace('#\A' . $routeKey . '\z#u', $redirectTo, $uri), - $this->collection->getRedirectCode($routeKey), - ); - } // Store our locale so CodeIgniter object can // assign it to the Request. if (str_contains($matchedKey, '{locale}')) { @@ -491,6 +477,25 @@ protected function checkRoutes(string $uri): bool unset($matched); } + // Is this route supposed to redirect to another? + if ($this->collection->isRedirect($matchedKey)) { + // replacing matched route groups with references: post/([0-9]+) -> post/$1 + $redirectTo = preg_replace_callback('/(\([^\(]+\))/', static function (): string { + static $i = 1; + + return '$' . $i++; + }, is_array($handler) ? key($handler) : $handler); + + if (str_contains($matchedKey, '{locale}')) { + $redirectTo = str_replace('{locale}', $this->detectedLocale, $redirectTo); + } + + throw new RedirectException( + preg_replace('#\A' . $routeKey . '\z#u', $redirectTo, $uri), + $this->collection->getRedirectCode($matchedKey), + ); + } + // Are we using Closures? If so, then we need // to collect the params into an array // so it can be passed to the controller method later. diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 6e22c95ed4f9..465143ebf125 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -976,6 +976,9 @@ public static function provideRedirectRoute(): iterable ['post/(:num)', 'article/(:num)', 'alias', '/article/1', 'post/1', 'alias'], ['post/(:num)/comment/(:any)', 'article/(:num)/(:any)', 'post/$1/comment/$2', '/article/1/2', 'post/1/comment/2', 'alias'], ['post/(:segment)/comment/(:any)', 'article/(:num)/(:any)', 'alias', '/article/1/2', 'post/1/comment/2', 'alias'], + ['posts', '{locale}/articles', 'posts', '/en/articles', 'posts', 'alias'], + ['{locale}/posts', '{locale}/articles', '{locale}/posts', '/en/articles', 'en/posts', 'alias'], + ['{locale}/posts', '{locale}/articles', 'alias', '/en/articles', 'en/posts', 'alias'], ]; } From cdeeb30ba4221f1b4c0f3d7e19d22e14243f1531 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:20:07 +0700 Subject: [PATCH 2/4] test(Router): cover redirect status code and locale edge cases --- tests/system/Router/RouterTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 465143ebf125..88f10ca54140 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -958,6 +958,7 @@ public function testRedirectRoute( $this->expectException(RedirectException::class); $this->expectExceptionMessage($expectedPath); + $this->expectExceptionCode(302); $router = new Router($collection, $this->request); $router->handle($url); @@ -979,9 +980,29 @@ public static function provideRedirectRoute(): iterable ['posts', '{locale}/articles', 'posts', '/en/articles', 'posts', 'alias'], ['{locale}/posts', '{locale}/articles', '{locale}/posts', '/en/articles', 'en/posts', 'alias'], ['{locale}/posts', '{locale}/articles', 'alias', '/en/articles', 'en/posts', 'alias'], + ['{locale}/post/(:num)', '{locale}/article/(:num)', '{locale}/post/$1', '/en/article/1', 'en/post/1', 'alias'], + ['{locale}/post/(:num)', '{locale}/article/(:num)', 'alias', '/en/article/1', 'en/post/1', 'alias'], ]; } + public function testRedirectRouteWithUnsupportedLocale(): void + { + $config = config(App::class); + $config->supportedLocales = ['en']; + + $collection = clone $this->collection; + $collection->resetRoutes(); + $collection->useSupportedLocalesOnly(true); + + $collection->get('{locale}/posts', 'Controller::method'); + $collection->addRedirect('{locale}/articles', '{locale}/posts'); + + $this->expectException(PageNotFoundException::class); + + $router = new Router($collection, $this->request); + $router->handle('/fr/articles'); + } + public function testRoutePlaceholderAnyWithMultipleSegmentsParamFalse(): void { $this->createRouteCollection(); From d471da8a4b1029b1e4970038936af78672930bd4 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:55:23 +0700 Subject: [PATCH 3/4] test(Router): test non-default redirect status code and clean up comment --- system/Router/Router.php | 1 - tests/system/Router/RouterTest.php | 10 ++++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/system/Router/Router.php b/system/Router/Router.php index 02426fcc9106..175935c34efd 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -477,7 +477,6 @@ protected function checkRoutes(string $uri): bool unset($matched); } - // Is this route supposed to redirect to another? if ($this->collection->isRedirect($matchedKey)) { // replacing matched route groups with references: post/([0-9]+) -> post/$1 $redirectTo = preg_replace_callback('/(\([^\(]+\))/', static function (): string { diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 88f10ca54140..591ac780cb7b 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -949,27 +949,28 @@ public function testRedirectRoute( string $url, string $expectedPath, string $alias, + int $status = 302, ): void { $collection = clone $this->collection; $collection->resetRoutes(); $collection->get($route, 'Controller::method', ['as' => $alias]); - $collection->addRedirect($redirectFrom, $redirectTo); + $collection->addRedirect($redirectFrom, $redirectTo, $status); $this->expectException(RedirectException::class); $this->expectExceptionMessage($expectedPath); - $this->expectExceptionCode(302); + $this->expectExceptionCode($status); $router = new Router($collection, $this->request); $router->handle($url); } /** - * @return iterable + * @return iterable */ public static function provideRedirectRoute(): iterable { - // [$route, $redirectFrom, $redirectTo, $url, $expectedPath, $alias] + // [$route, $redirectFrom, $redirectTo, $url, $expectedPath, $alias, $status] return [ ['posts', 'articles', 'posts', '/articles', 'posts', 'alias'], ['posts', 'articles', 'alias', '/articles', 'posts', 'alias'], @@ -979,6 +980,7 @@ public static function provideRedirectRoute(): iterable ['post/(:segment)/comment/(:any)', 'article/(:num)/(:any)', 'alias', '/article/1/2', 'post/1/comment/2', 'alias'], ['posts', '{locale}/articles', 'posts', '/en/articles', 'posts', 'alias'], ['{locale}/posts', '{locale}/articles', '{locale}/posts', '/en/articles', 'en/posts', 'alias'], + ['{locale}/posts', '{locale}/articles', '{locale}/posts', '/en/articles', 'en/posts', 'alias', 301], ['{locale}/posts', '{locale}/articles', 'alias', '/en/articles', 'en/posts', 'alias'], ['{locale}/post/(:num)', '{locale}/article/(:num)', '{locale}/post/$1', '/en/article/1', 'en/post/1', 'alias'], ['{locale}/post/(:num)', '{locale}/article/(:num)', 'alias', '/en/article/1', 'en/post/1', 'alias'], From c4b50388fb9a6ca0a24b7797e1b21ad490f4b270 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:00:25 +0700 Subject: [PATCH 4/4] docs: add changelog entry for redirect route {locale} fix --- docs/changelogs/v4.7.7.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 367c69f9cb37..87721455199d 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -26,6 +26,7 @@ Release Date: Unreleased - **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access. - **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names. - **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format. +- **Routing:** Fixed a bug where redirect routes containing the `{locale}` placeholder were not recognized as redirects and did not replace `{locale}` in the redirect target. - **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests. - **Session:** Fixed a bug where `BaseHandler::destroyCookie()` did not send the `SameSite` attribute in the `Set-Cookie` header, which could prevent browsers from properly clearing the session cookie. - **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop.