簡體   English   中英

使用漂亮URL中的查詢字符串進行Mod_Rewrite

[英]Mod_Rewrite with Query Strings In Pretty URLs

我有一個.htaccess文件,該文件使用路由查詢字符串參數將所有URL重定向到index.php文件:

# 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]參數從未傳遞給主腳本。

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

只需添加[QSA]作為標記:

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

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

這是我的標准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>

設置標志QSA-查詢字符串追加

# Enable Mod_rewrite
RewriteEngine on

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

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

Apache Mod_Rewrite文檔具有更多關於標志的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM