diff --git a/system/HTTP/RedirectResponse.php b/system/HTTP/RedirectResponse.php index ad9f70b51cee..a2e31a866483 100644 --- a/system/HTTP/RedirectResponse.php +++ b/system/HTTP/RedirectResponse.php @@ -91,7 +91,7 @@ public function route(string $route, array $params = [], int $code = 302, string throw HTTPException::forInvalidRedirectRoute($route); } - return $this->redirect(base_url($route), $method, $code); + return $this->redirect(site_url($route), $method, $code); } /** diff --git a/tests/system/HTTP/RedirectResponseTest.php b/tests/system/HTTP/RedirectResponseTest.php index ec23c3a2da99..d452444236fe 100644 --- a/tests/system/HTTP/RedirectResponseTest.php +++ b/tests/system/HTTP/RedirectResponseTest.php @@ -3,6 +3,7 @@ namespace CodeIgniter\HTTP; use Config\App; +use CodeIgniter\Config\Config; use CodeIgniter\Config\Services; use CodeIgniter\Validation\Validation; use CodeIgniter\Router\RouteCollection; @@ -67,6 +68,27 @@ public function testRedirectRoute() $this->assertEquals('http://example.com/exampleRoute', $response->getHeaderLine('Location')); } + public function testRedirectRouteBaseUrl() + { + $config = new App(); + $config->baseURL = 'http://example.com/test/'; + Config::injectMock('App', $config); + + $request = new MockIncomingRequest($config, new URI('http://example.com/test/'), null, new UserAgent()); + Services::injectMock('request', $request); + + $response = new RedirectResponse(new App()); + + $this->routes->add('exampleRoute', 'Home::index'); + + $response->route('exampleRoute'); + + $this->assertTrue($response->hasHeader('Location')); + $this->assertEquals('http://example.com/test/index.php/exampleRoute', $response->getHeaderLine('Location')); + + Config::reset(); + } + public function testRedirectRouteBad() { $this->expectException(Exceptions\HTTPException::class);