2014-03-02 76 views
0

我正在尝试设置一个小的路由系统,但是当我将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文件的URL数据呼应内容

简单的路由脚本

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] 
+0

你可以检查你的* AcceptPathInfo *配置吗? – Gumbo

+0

在您的RewriteCond中,您只是__ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _部件,然后您使用该部分重写为 - 因此_you_放弃原始请求URI中的index.php后面的任何内容...顺便说一句,我真的不明白'($ | \ | \?)'应该达到什么样的模式,请注意解释一下吗?) – CBroe

+0

我不确定PATH_INFO甚至可以在这种情况下工作,因为这个值是任何在可能与_physically_存在的文件匹配的部分之后出现的路径 - 但是正是这个物理存在的文件在这里尝试取出来...... – CBroe

回答

1

你没有一条规则告诉/public/hello这样的请求被路由到index.php文件。 Apache和PATH INFO不够聪明来弄清楚。你需要明确告诉Apache你想要一个请求路由到脚本:

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

我已经重写了我的htaccess文件,因为它驻留在我的公用文件夹中。检查更新#1 它的工作原理,但它不会从我的$ routes数组中获取斜线(/),有关修复该问题的任何帮助? –

+0

@ Stephan-v不知道你为什么输掉斜线,你的脚本和htaccess文件[适合我](http://i.stack.imgur.com/uX7S0.png) –

相关问题