![](/img/trans.png)
[英]PHP routing - How can I implement 404 page on wrong url routes?
[英]URL Routing in PHP : url routes ony to 404-Not-Found page
我在名为“das-xampp”的 htdocs 文件夹中有一个 php 项目。 根目录中的索引文件用作路由器。 我在视图中有另一个 index.php。 所以结构如下:
das-xamp
|__index.php
|__views
|__index.php
|__about-us.php
|__404-Not_Found.php
因此,每当有人键入“localhost/das-xampp”时,它应该将用户重新路由到“views/index.php”内的索引我的根索引(用作路由器的那个)如下:
<?php
$path = trim($_SERVER['REQUEST_URI'], '/');
parse_url($path, PHP_URL_PATH);
$routes = [
''=> 'views/index.php',
'about-us' => 'views/about-us.php'
];
if (array_key_exists($path,$routes)) {
require $routes[$path];
}else {
require 'views/404-Not-Found.php';
}
?>
问题是每当我输入“localhost/das-xampp”(打开 apache 和 mysql 后),就会出现未找到的 php。 即使我手动输入'localhost/das-xampp/about-us' Object not found 也会显示。
如果我使用,这不会发生
"php -S localhost:<some_digit>"
我所有的观点都很好。
问题出在 .htaccess 文件上。 我没有包含 .htaccess 文件,该文件将根据我在 index.php 文件(根目录中的那个)中的路由重新路由。 我在下面发布 .htaccess 代码
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /das/index.php [L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.