world-game/site/controllers/error.php
isUnknown 82e69c7259
All checks were successful
Deploy Production / Deploy to Production (push) Successful in 26s
fix: redirects 301 not firing — three bugs
- toPages()->first() instead of toPage() (pages field stores YAML array)
- Force read from default language content to avoid empty rules on FR requests
- Add error.php template so Kirby uses the error controller (without it,
  Kirby fell back to default template and ignored site/controllers/error.php)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-01 10:01:49 +02:00

38 lines
1.2 KiB
PHP

<?php
return function ($kirby, $page, $site) {
$path = $kirby->path();
if (!str_ends_with($path, '.json')) {
$normalized = '/' . rtrim(strtolower($path), '/');
$cache = $kirby->cache('redirects');
$map = $cache->get('map');
if ($map === null) {
$map = [];
$redirectsPage = $site->find('redirections');
if ($redirectsPage) {
$defaultLang = $kirby->defaultLanguage()?->code() ?? 'fr';
$rules = $redirectsPage->content($defaultLang)->rules()->toStructure();
foreach ($rules as $rule) {
$from = trim($rule->from()->value());
if ($from === '') continue;
$key = '/' . trim(strtolower($from), '/');
$target = $rule->to()->toPages()->first();
if ($target) {
$map[$key] = $target->url();
}
}
}
$cache->set('map', $map, 1440);
}
if (isset($map[$normalized])) {
go($map[$normalized], 301);
}
}
$kirby->response()->code(404);
return [];
};