I using below PHP & htacess codes for routing structure in my project, I would like to add forced trailing slash to all routes like that;
localhost/project/about-us
> localhost/project/about-us/
It's working on main path like localhost/project/
so I cannot display main path without trailing slash like localhost/project
because it's redirect me(that's what I want for all routes).
How can I do that? Thanks. Codes:
function request_path()
{
$request_uri = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
$script_name = explode('/', trim($_SERVER['SCRIPT_NAME'], '/'));
$parts = array_diff_assoc($request_uri, $script_name);
if (empty($parts))
{
return '/';
}
$path = implode('/', $parts);
if (($position = strpos($path, '?')) !== FALSE)
{
$path = substr($path, 0, $position);
}
return $path;
}
$routes = [
'/' => function()
{
echo 'home';
},
'about-us' => function()
{
echo 'about';
},
'profile/edit' => function()
{
echo 'profile edit';
}
];
$path = request_path();
if (isset($routes[$path]) AND is_callable($routes[$path]))
{
$routes[$path]();
}
else
{
echo 404;
}
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]