diff --git a/src/Http/Environment.php b/src/Http/Environment.php index 413f07ca08..bb8f4f6521 100644 --- a/src/Http/Environment.php +++ b/src/Http/Environment.php @@ -602,13 +602,7 @@ protected function detectPortInHost(string|null $host = null): array */ protected function detectRequestUri(string|null $requestUri = null): Uri { - // make sure the URL parser works properly when there's a - // colon in the request URI but the URI is relative - if (Url::isAbsolute($requestUri) === false) { - $requestUri = 'https://getkirby.com' . $requestUri; - } - - $uri = new Uri($requestUri); + $uri = new Uri($requestUri ?? ''); // create the URI object as a combination of base uri parts // and the parts from REQUEST_URI diff --git a/src/Http/Uri.php b/src/Http/Uri.php index 08c4618af3..9cd951cfcf 100644 --- a/src/Http/Uri.php +++ b/src/Http/Uri.php @@ -88,7 +88,16 @@ class Uri public function __construct(array|string $props = [], array $inject = []) { if (is_string($props) === true) { - $props = parse_url($props); + // make sure the URL parser works properly when there's a + // colon in the string but the string is a relative URL + if (Url::isAbsolute($props) === false) { + $props = 'https://getkirby.com/' . $props; + $props = parse_url($props); + unset($props['scheme'], $props['host']); + } else { + $props = parse_url($props); + } + $props['username'] = $props['user'] ?? null; $props['password'] = $props['pass'] ?? null; diff --git a/tests/Http/UriTest.php b/tests/Http/UriTest.php index 4a1cf0829c..b6453a0ab0 100644 --- a/tests/Http/UriTest.php +++ b/tests/Http/UriTest.php @@ -325,6 +325,15 @@ public static function buildProvider(): array '/search/page:2?q=something' ], + // relative path with colon + adding query + [ + '/search/page:2', + [ + 'query' => ['q' => 'something'] + ], + '/search/page:2?q=something' + ], + // path + adding params + query [ 'https://getkirby.com/search',