Skip to content

Commit

Permalink
fix: custom 404 error page with multiple languages
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed May 21, 2024
1 parent 326aa2f commit 7eb0547
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions resources/server/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,31 @@
// HTTP response: 404
if ((realpath($filename) === false || strpos(realpath($filename), realpath($_SERVER['DOCUMENT_ROOT'])) !== 0) || !file_exists($filename) || is_dir($filename)) {
http_response_code(404);

// favicon.ico
if ($path == '/favicon.ico') {
header('Content-Type: image/vnd.microsoft.icon');

return logger(false);
}

// 404.html exists?
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . ERROR_404)) {
// is 404.html exists?
$path404 = '';
$subDir = '';
if (file_exists($_SERVER['DOCUMENT_ROOT'] . ERROR_404)) {
$path404 = ERROR_404;
}
// is 404.html exists in a (language) sub dir?
$pathAsArray = explode('/', $path);
if (count($pathAsArray) > 2) {
$subDir = '/' . $pathAsArray[1];
if (file_exists($_SERVER['DOCUMENT_ROOT'] . $subDir . ERROR_404)) {
$path404 = $subDir . ERROR_404;
}
}

// default 404 page
if (empty($path404)) {
echo <<<END
<!doctype html>
<html>
Expand All @@ -84,8 +100,9 @@

return logger(true);
}
$path = ERROR_404;
$filename = $_SERVER['DOCUMENT_ROOT'] . ERROR_404;

$path = $path404;
$filename = $_SERVER['DOCUMENT_ROOT'] . $path404;
}

// HTTP response: 200
Expand Down

0 comments on commit 7eb0547

Please sign in to comment.