25 lines
977 B
PHP
25 lines
977 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
// Router for PHP's built-in development server. Apache protects the legacy
|
|
// directory with .htaccess, but the built-in server ignores it completely.
|
|
$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;
|