我是 PHP 的菜鸟,我正在使用 AltoRouter 设置一个简单的路由。下面是我的 index.php 和 .htaccess 文件,它们位于路由文件夹中,即 /var/www/html/ 我正在使用 Apache2 来提供网页服务。
索引.php
<?php
require 'vendor/AltoRouter.php';
$router = new AltoRouter();
// map homepage
$router->map('GET', '/', function () {
require __DIR__ . '/views/home.php';
});
$router->map('GET|POST', '/login', function () {
require __DIR__ . '/views/login.php';
});
$router->map('GET', '/signup', function () {
require __DIR__ . '/views/signup.php';
});
$match = $router->match();
// call closure or throw 404 status
if ($match && is_callable($match['target'])) {
call_user_func_array($match['target'], $match['params']);
} else {
// no route was matched
header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}
?>
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
问题:当我访问 localhost 时,'home.php' 得到了服务,但是当我访问 'localhost/login' 或 'localhost/signup' 时,我得到 404 错误。