23 lines
734 B
PHP
23 lines
734 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
// Router for the Lighthouse static server.
|
|
// PHP's built-in server answers requests for missing files with index.html,
|
|
// which makes Lighthouse read HTML as robots.txt. Return a real 404 instead.
|
|
|
|
$path = rawurldecode((string) parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH));
|
|
$root = (string) realpath($_SERVER['DOCUMENT_ROOT'] ?? '.');
|
|
$target = realpath($root . $path);
|
|
|
|
$insideRoot = $target !== false
|
|
&& ($target === $root || str_starts_with($target, $root . DIRECTORY_SEPARATOR));
|
|
|
|
if ($insideRoot && (is_file($target) || is_file($target . '/index.html'))) {
|
|
return false;
|
|
}
|
|
|
|
http_response_code(404);
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
echo "404 Not Found\n";
|