2013-05-02 63 views
1

我在我的網頁上有一個重寫規則。我的RewriteRule有什麼問題?它似乎沒問題,但它不起作用

RewriteEngine On 

RewriteRule ^(.*) index.php?p=$1 [L] 

我希望它的工作,因此重寫的URL是這樣的:

http://example.com   -> index.php 
http://example.com/home  -> index.php?p=home 
http://example.com/lol  -> index.php?p=lol 

但是,當我用我的index.php裏面下面的PHP代碼

print_r($_GET) 

它給出了這樣的:

Array ([p] => index.php) 

它給出了相同的結果在所有的URL(我試過這些:http://example.com,http://example.com/,http://example.com/about,http://example.com/about/

你能幫我debig這個嗎?

+0

什麼? 'mod_rewrite'離題在這裏?我們甚至有一個標籤維基! – 2013-05-02 10:56:22

回答

0

我想通弄明白了:

正確的代碼是這樣的:

RewriteEngine On 
RewriteRule ^([^.]+)/?$ index.php?p=$1 [NC,L] 

對不起,我的問題。

+0

這可以防止您在網址中使用點。不知道功能的錯誤... – 2013-05-02 10:58:51

0

的問題是,你重寫URL仍然符合規則,你會得到一個新的重寫:

http://example.com/home 
http://example.com/index.php?p=home 
http://example.com/index.php?p=index.php 

由於[QSA]標誌未設置,新p參數取代了以前的一個。 (我不完全確定你爲什麼沒有無限循環,我想mod_rewrite會進行檢查以避免無用的重定向)。

您需要添加附加條件。例如,只有當URL與物理文件或目錄不匹配時,纔可以進行重寫:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php?p=$1 [L] 
相關問題