2014-03-02 122 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) –

相關問題