/** * This method attempts to detect the base uri. * Only the PATH_INFO variable is considered. * * If this variable is not set, the root (/) is assumed. * * @return string */ function guessBaseUri() { $pathInfo = $this->httpRequest->getRawServerValue('PATH_INFO'); $uri = $this->httpRequest->getRawServerValue('REQUEST_URI'); // If PATH_INFO is found, we can assume it's accurate. if (!empty($pathInfo)) { // We need to make sure we ignore the QUERY_STRING part if ($pos = strpos($uri, '?')) $uri = substr($uri, 0, $pos); // PATH_INFO is only set for urls, such as: /example.php/path // in that case PATH_INFO contains '/path'. // Note that REQUEST_URI is percent encoded, while PATH_INFO is // not, Therefore they are only comparable if we first decode // REQUEST_INFO as well. $decodedUri = URLUtil::decodePath($uri); // A simple sanity check: if (substr($decodedUri, strlen($decodedUri) - strlen($pathInfo)) === $pathInfo) { $baseUri = substr($decodedUri, 0, strlen($decodedUri) - strlen($pathInfo)); return rtrim($baseUri, '/') . '/'; } throw new Exception('The REQUEST_URI (' . $uri . ') did not end with the contents of PATH_INFO (' . $pathInfo . '). This server might be misconfigured.'); } // The last fallback is that we're just going to assume the server root. return '/'; }