Skip to content

Commit 100705e

Browse files
authored
Avoid full bootstrap for 404s on static asset requests (#583)
1 parent f2cbf37 commit 100705e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

public/index.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@
1313
define('MAHO_ROOT_DIR', dirname(__DIR__));
1414
define('MAHO_PUBLIC_DIR', __DIR__);
1515

16+
// Early exit for missing static assets — avoids full bootstrap just to return a 404.
17+
// Web servers should handle this natively, but this is a safety net for environments
18+
// where that's not configured (php -S dev server, FrankenPHP defaults, shared hosting).
19+
$requestPath = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?: '/';
20+
if ($requestPath !== '/') {
21+
$ext = strtolower(pathinfo($requestPath, PATHINFO_EXTENSION));
22+
$staticExts = [
23+
'jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'svg', 'svgz', 'ico', 'bmp', 'apng',
24+
'css', 'js', 'mjs', 'map', 'woff', 'woff2', 'ttf', 'otf', 'eot',
25+
'mp3', 'mp4', 'ogg', 'webm', 'wav', 'flac', 'aac', 'm4a', 'm4v', 'ogv', 'mov',
26+
'pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx',
27+
'zip', 'gz', 'tar', 'rar', '7z',
28+
];
29+
if (in_array($ext, $staticExts, true)) {
30+
http_response_code(404);
31+
exit;
32+
}
33+
}
34+
1635
require MAHO_ROOT_DIR . '/vendor/autoload.php';
1736

1837
#\Maho\Profiler::enable();

0 commit comments

Comments
 (0)