由于mod_rewrite,$ _SERVER ['PATH_INFO]变量失败

时间:2014-03-02 16:08:05

标签: php regex .htaccess mod-rewrite pathinfo

我目前正在尝试设置一个小型路由系统,但是当我将public / index.php重写为public / my $ _SERVER ['PATH_INFO]变量时,无法获得任何输入。

当我访问时,没有.htaccess文件,它工作正常:

公开/ index.php的/你好

我明白了:

欢迎您!这是主页。

但是当我重写删除index.php并且只留下公众/我死在水中时。

有人知道这方面的解决办法,还是有人可以给我解释一下?

基于跟踪脚本的网址数据回显内容的简单路由脚本

   <?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]

1 个答案:

答案 0 :(得分:1)

您没有一条规则可以将/public/hello之类的请求路由到index.php文件。 Apache和PATH INFO不够聪明,无法解决这个问题。您需要明确告诉apache您希望将请求路由到脚本:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^public/(.*)$ /public/index.php/$1 [L]