This file when has process parameter in url shows 404 page, but when i parse_url im getting page, but with parameter, even when i header location it. How can i fix this error to show only friendly url and when parameter occur it just removed it and shows friendy_url?
Assistant
To address the issue of showing a 404 page when there are extra parameters in the URL, you need to validate the URL and strip out these parameters for your routing logic. Let's go through a solution step-by-step:
Parse and Normalize the URL: Extract the path part of the URL and ignore any query parameters.
Check Path vs Config: Use the cleaned path to match against the configurations to find the appropriate page.
Here’s a modified version of your script to achieve this:
php
<?php
// Importuj plik konfiguracyjny
require_once 'config.php';
// Parse the URL to get the path without query parameters
$urlComponents = parse_url($_SERVER['REQUEST_URI']);
$path = rtrim($urlComponents['path'], '/'); // Ensure no trailing slash
// Sprawdź dopasowanie ścieżki do przyjaznych adresów URL
$currentPage = null;
$currentScript = null;
$currentTemplate = null;
// Find the page configuration for the given path
function findPageConfig($path, $config){
foreach ($config as $page => $settings) {
if ($settings['url'] === $path) {
return $settings;
}
}
return null;
}
$pageConfig = findPageConfig($path, $config);
if ($pageConfig !== null) {
$currentPage = array_search($pageConfig, $config);
$currentScript = ABS_PATH . $pageConfig['script'];
$currentTemplate = null;
// Ustawienie szablonu na podstawie klucza 'template' lub 'pageType'
if (isset($pageConfig['template'])) {
$currentTemplate = ABS_PATH . $pageConfig['template'];
} elseif (isset($pageConfig['pageType']) && isset($defaultTemplates[$pageConfig['pageType']]['template'])) {
$currentTemplate = ABS_PATH . $defaultTemplates[$pageConfig['pageType']]['template'];
} else {
$currentTemplate = ABS_PATH . $defaultTemplates['page']['template'];
}
} else {
// If no matching page config, redirect to 404
header("Location: /404/");
exit;
}
if ($isBufor) {
ob_start();
}
// Import the header
require_once ABS_PATH . 'core/header.php';
// Import the template
require_once $currentTemplate;
// Determine if the script exists and matches the current config
if ($pageConfig !== null && $pageConfig['url'] === $path && file_exists($currentScript)) {
// Import the content page
require_once $currentScript;
} else {
// Redirect to 404 if page config doesn't match
require_once '404.php';
exit;
}
// Import the footer
require_once ABS_PATH . 'core/footer.php';
if ($isBufor) {
// Get the buffer content
$content = ob_get_clean();
// Modify the buffer content if needed
$modifiedContent = processContent($content);
// Output the modified content
echo $modifiedContent;
}
?>
Explanation:
Path Parsing: We use parse_url to safely extract just the path part of the URL.
Path Matching: Ensure the findPageConfig function looks up the path without query parameters.
Trailing Slashes: The rtrim($urlComponents['path'], '/') removes any trailing slash to make sure paths like /example/ and /example are treated equally.
By normalizing and strictly matching the path, you avoid issues with extraneous parameters causing 404 errors, and only friendly URLs are considered.