2012-06-07 83 views
1

我有一個重定向所有URL的index.php文件與路徑查詢字符串參數.htaccess文件:mod_rewrite的與查詢字符串在漂亮的URL

# Enable Mod_rewrite 
RewriteEngine on 

# Redirect pretty URLs to the index file 
RewriteRule ^([\w\/\-]+)/?$ index.php?route=$1 

這個偉大工程,具有URL的異常,並查詢它們中的字符串。例如,我有一個表單重定向回到登錄屏幕,出現錯誤代碼(/admin/login?error=1)。顯然,問題是,$_GET[error]參數永遠不會傳遞給主腳本。

我將如何獲得傳遞給腳本的這些參數?

回答

7

只需添加[QSA]作爲一個標誌:

「qsappend | QSA」(查詢字符串追加)此標記強制重寫 引擎替換字符串的查詢字符串的一部分追加到 現有的字符串,而不是取代它。當您想要 通過重寫規則將更多數據添加到查詢字符串時使用此選項。

RewriteRule ^([\w\/\-]+)/?$ index.php?route=$1 [QSA]

+0

非常酷,不知道那個標誌存在。謝謝:) –

+0

你只是打敗了我勞倫斯:)有一個upvote。 –

2

設置標誌QSA - 查詢字符串附加

# Enable Mod_rewrite 
RewriteEngine on 

# Redirect pretty URLs to the index file 
RewriteRule ^([\w\/\-]+)/?$ index.php?route=$1 [QSA] 

(我所做的只是添加[QSA])

Apache的mod_rewrite的文檔中有更多關於標誌。

+0

非常感謝! :) –

3

這是我的標準mod_rewrite的條目(改變你的問題)。我將它添加到任何潛在的疼痛中,例如圖像&包括。它重定向除了現有文件以外的所有內容。

<IfModule mod_rewrite.c> 
    Options +FollowSymlinks 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([\w\/\-]+)/?$ index.php?route=$1 [QSA] 
</IfModule> 
+0

感謝您的幫助片段:) –

+0

這正是我一直在尋找的。謝謝。 – Jeff