0

我目前正在尝试建立一个小路由系统,但是当我将我的 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]
4

1 回答 1

1

您没有规则告诉请求/public/hello被路由到 index.php 文件。Apache 和 PATH INFO 不够聪明,无法弄清楚这一点。您需要明确告诉 apache 您希望将请求路由到脚本:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^public/(.*)$ /public/index.php/$1 [L]
于 2014-03-02T17:36:46.397 回答