2016-08-07 29 views
0

我想根據其部分查詢字符串重寫以下URL,但同時我想保留查詢字符串的其餘部分。我如何才能部分重寫基於查詢字符串的URL

原始地址:

http://example.com/index.php?route=product/show&item_id=25&show_mobile=true

轉換爲:

http://example.com/product/show/?item_id=25&show_mobile=true

我已經搜查,發現在Apache以下維基:

https://wiki.apache.org/httpd/RewriteQueryString

但它沒有在Query字符串中重寫單個密鑰&值對並保留其餘部分的部分。

回答

1

這應該讓你關閉,它基於wiki中的「刪除鍵」條目。您希望隔離將在第一次捕獲中進入路徑的部分,併爲剩餘的查詢字符串使用第二次捕獲。

RewriteCond %{QUERY_STRING} ^(route=[^&]*)&(.*) 
RewriteRule ^index.php /%1?%2 
0

您可以使用此通用規則來捕獲的route參數值從查詢字符串中的任何命令和目標URL重用:

RewriteEngine On 

RewriteCond %{THE_REQUEST} \?(.*&)?route=([^&]*)&?(\S*)\sHTTP [NC] 
RewriteRule ^index\.php$ /%2?%1%3 [R=301,NE,L]