私は PHP の初心者です。AltoRouter を使用して簡単なルーティングを設定しています。以下は、ルートフォルダー、つまり /var/www/html/ にある私の index.php および .htaccess ファイルです。私は Web ページを提供するために Apache2 を使用しています。
index.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 エラーが発生します。