map/scripts/router.php
2026-08-11 21:51:07 +09:00

25 lines
976 B
PHP

<?php
declare(strict_types=1);
// Router for PHP's built-in development server. Reject dot-prefixed path
// segments explicitly because the built-in server does not apply .htaccess.
$uri = rawurldecode((string)(parse_url((string)($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH) ?: '/'));
$segments = array_values(array_filter(explode('/', str_replace('\\', '/', $uri)), static fn(string $part): bool => $part !== ''));
foreach ($segments as $segment) {
if (str_starts_with($segment, '.')) {
http_response_code(404);
header('Content-Type: text/plain; charset=utf-8');
echo "Not found\n";
return true;
}
}
$projectRoot = dirname(__DIR__);
$relative = ltrim($uri, '/');
$candidate = $projectRoot . ($relative === '' ? DIRECTORY_SEPARATOR . 'index.html' : DIRECTORY_SEPARATOR . $relative);
if (is_file($candidate)) return false;
http_response_code(404);
header('Content-Type: text/plain; charset=utf-8');
echo "Not found\n";
return true;