diff --git a/src/Router.php b/src/Router.php index e4c2eab..b3c50b7 100644 --- a/src/Router.php +++ b/src/Router.php @@ -686,11 +686,23 @@ public static function setBasePath($serverBasePath) */ public static function getCurrentUri(): string { - // Get the current Request URI and remove rewrite base path from it (= allows one to run the router in a sub folder) - $uri = substr(rawurldecode($_SERVER['REQUEST_URI']), strlen(static::getBasePath())); + $basePath = static::getBasePath(); + $requestUri = rawurldecode($_SERVER['REQUEST_URI']); + + // Early exit If base path doesn't match + if (strncmp($requestUri, $basePath, strlen($basePath)) !== 0) { + if (!static::$notFoundHandler) { + static::$notFoundHandler = function () { + \Leaf\Exception\General::default404(); + }; + } + static::invoke(static::$notFoundHandler); + } - if (strstr($uri, '?')) { - $uri = substr($uri, 0, strpos($uri, '?')); + // Get the current Request URI and remove rewrite base path from it (= allows one to run the router in a sub folder) + $uri = substr($requestUri, strlen($basePath)) ?: '/'; + if (($queryPos = strpos($uri, '?')) !== false) { + $uri = substr($uri, 0, $queryPos); } return '/' . trim($uri, '/');