2014-02-19 43 views
0

我想通過url傳遞變量,而不包括?var1 = test,但用/ test取代了。我有以下代碼:通過使用斜槓網址傳遞參數

$pathinfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : $_SERVER['REDIRECT_URL'];  
$params = preg_split('|/|', $pathinfo, -1, PREG_SPLIT_NO_EMPTY); 
print_r($params); 

這個問題是,如果我把它放在index.php文件中,我必須包括文件名。

我得到什麼: http://example.com/index.php/test

我想要什麼: http://example.com/test

我該怎麼辦?

+0

這被稱爲URL重寫。看看它。 http://stackoverflow.com/questions/16388959/url-rewriting-with-php –

回答

2

在你的根文件夾中添加一個.htaccess文件並粘貼此代碼

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

只要確保mod_rewrite的啓用

0

What should I do?

使用使用mod_rewrite.htaccess重寫規則。您的規則可能看起來像:

(例如,從WordPress的):

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f # Existing File 
RewriteCond %{REQUEST_FILENAME} !-d # Existing Directory 
RewriteRule . /index.php [L] 

更多的例子here和一些更 「簡單」 的解釋here

0

嘗試在您對此內容的根目錄創建一個名爲.htaccess文件:

<IfModule mod_rewrite.c> 
    RewriteEngine On 

    # Redirect Trailing Slashes... 
    RewriteRule ^(.*)/$ /$1 [L,R=301] 

    # Handle Front Controller... 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule^index.php [L] 
</IfModule> 

還要確保modrewrite已啓用。