diff --git a/api/index.php b/api/index.php index 7920248..de49fec 100644 --- a/api/index.php +++ b/api/index.php @@ -57,6 +57,18 @@ function respond(int $code, array $data): void { function parse_path(): array { $uri = $_SERVER['REQUEST_URI'] ?? '/'; $path = parse_url($uri, PHP_URL_PATH); + + // Detect and strip the base path (for subdirectory deployments) + $scriptName = $_SERVER['SCRIPT_NAME'] ?? '/index.php'; + // SCRIPT_NAME for /protocol-droid/api/index.php is /protocol-droid/api/index.php + // Go up two levels to get the base path: dirname(dirname(...)) = /protocol-droid + $basePath = rtrim(str_replace('\\', '/', dirname(dirname($scriptName))), '/'); + if ($basePath === '/' || $basePath === '.') $basePath = ''; + if ($basePath && strpos($path, $basePath) === 0) { + $path = substr($path, strlen($basePath)); + } + + // Strip /api prefix $path = preg_replace('#^/api#', '', $path); $path = trim($path, '/'); return $path === '' ? [] : explode('/', $path);