我目前正在尝试建立一个小路由系统,但是当我将我的 public/index.php 重写为 public/ 我的 $_SERVER['PATH_INFO] 变量无法获得任何输入。
当我访问时,它可以在没有 .htaccess 文件的情况下正常工作:
公共/index.php/你好
我得到:
欢迎!这是主页。
但是当我重写以删除 index.php 并且只保留公共/时,我已经死在水中了。
有谁知道这个问题的解决方法,或者有人可以给我一个解释吗?
简单的路由脚本,根据脚本后面的 url 数据回显内容
<?php
// First, let's define our list of routes.
// We could put this in a different file and include it in order to separate
// logic and configuration.
$routes = array(
'/' => 'Welcome! This is the main page.',
'/hello' => 'Hello, World!',
'/users' => 'Users!'
);
// This is our router.
function router($routes)
{
// Iterate through a given list of routes.
foreach ($routes as $path => $content) {
if ($path == $_SERVER['PATH_INFO']) {
// If the path matches, display its contents and stop the router.
echo $content;
return;
}
}
// This can only be reached if none of the routes matched the path.
echo 'Sorry! Page not found';
}
// Execute the router with our list of routes.
router($routes);
?>
我的 .HTACCESS 文件
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
更新#1
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]