URL PHP 中的路由:url 只路由到 404-Not-Found 页面
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' 也会显示找不到对象。
如果我使用
就不会发生这种情况
"php -S localhost:<some_digit>"
我的所有视图都正常。
not-found page
问题出在 .htaccess 文件上。我没有包含一个 .htaccess 文件,该文件将 re-route 根据我在 index.php 文件(根目录中的那个)中的路由。
我在下面发布 .htaccess 代码
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /das/index.php [L]
我在名为“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' 也会显示找不到对象。
如果我使用
就不会发生这种情况"php -S localhost:<some_digit>"
我的所有视图都正常。
not-found page
问题出在 .htaccess 文件上。我没有包含一个 .htaccess 文件,该文件将 re-route 根据我在 index.php 文件(根目录中的那个)中的路由。 我在下面发布 .htaccess 代码
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /das/index.php [L]