Fix page name parsing to support dots in filenames

- Change regex to only remove file extensions (.md, .php, .html) not all dots
- Fix en.test.md and nl.testpagina.md 404 errors caused by over-aggressive regex
- Update all references to use  instead of
- Language-specific pages with dots in names now load correctly
- Pages like 'en.test.md' work as expected without being truncated to 'en'
This commit is contained in:
Edwin Noorlander 2025-11-22 16:33:16 +01:00
parent bf2ee9c212
commit 14a6cae499

View File

@ -230,9 +230,10 @@ class CodePressCMS {
} }
$page = $_GET['page'] ?? $this->config['default_page']; $page = $_GET['page'] ?? $this->config['default_page'];
$page = preg_replace('/\.[^.]+$/', '', $page); // Only remove file extension at the end, not all dots
$pageWithoutExt = preg_replace('/\.(md|php|html)$/', '', $page);
$filePath = $this->config['content_dir'] . '/' . $page; $filePath = $this->config['content_dir'] . '/' . $pageWithoutExt;
$actualFilePath = null; $actualFilePath = null;
// Check for exact file matches first // Check for exact file matches first
@ -261,21 +262,21 @@ class CodePressCMS {
if (!isset($result)) { if (!isset($result)) {
$langPrefix = $this->currentLanguage; $langPrefix = $this->currentLanguage;
if (file_exists($this->config['content_dir'] . '/' . $langPrefix . '.' . $page . '.md')) { if (file_exists($this->config['content_dir'] . '/' . $langPrefix . '.' . $pageWithoutExt . '.md')) {
$actualFilePath = $this->config['content_dir'] . '/' . $langPrefix . '.' . $page . '.md'; $actualFilePath = $this->config['content_dir'] . '/' . $langPrefix . '.' . $pageWithoutExt . '.md';
$result = $this->parseMarkdown(file_get_contents($actualFilePath)); $result = $this->parseMarkdown(file_get_contents($actualFilePath));
} elseif (file_exists($this->config['content_dir'] . '/' . $langPrefix . '.' . $page . '.php')) { } elseif (file_exists($this->config['content_dir'] . '/' . $langPrefix . '.' . $pageWithoutExt . '.php')) {
$actualFilePath = $this->config['content_dir'] . '/' . $langPrefix . '.' . $page . '.php'; $actualFilePath = $this->config['content_dir'] . '/' . $langPrefix . '.' . $pageWithoutExt . '.php';
$result = $this->parsePHP($actualFilePath); $result = $this->parsePHP($actualFilePath);
} elseif (file_exists($this->config['content_dir'] . '/' . $langPrefix . '.' . $page . '.html')) { } elseif (file_exists($this->config['content_dir'] . '/' . $langPrefix . '.' . $pageWithoutExt . '.html')) {
$actualFilePath = $this->config['content_dir'] . '/' . $langPrefix . '.' . $page . '.html'; $actualFilePath = $this->config['content_dir'] . '/' . $langPrefix . '.' . $pageWithoutExt . '.html';
$result = $this->parseHTML(file_get_contents($actualFilePath)); $result = $this->parseHTML(file_get_contents($actualFilePath));
} }
} }
// If no file found, check if it's a directory // If no file found, check if it's a directory
if (!isset($result) && is_dir($filePath)) { if (!isset($result) && is_dir($filePath)) {
return $this->getDirectoryListing($page, $filePath); return $this->getDirectoryListing($pageWithoutExt, $filePath);
} }
if (isset($result) && $actualFilePath) { if (isset($result) && $actualFilePath) {