2011-02-16 64 views
0

我需要重定向簡化:如何使用mod_rewrite交換URL參數並丟棄其他參數?

/search.html?param1=val1&param2=val2&param3=val3&location=UK 

要:

/search/United-Kingdom/arg4=val2&arg5=val1 

的參數可以是任何順序或丟失,位置是一個代碼,在一個映射文件擴展(UK英國等)。

如果沒有匹配參數重定向到:

/search-info/ 

當前的位置展開代碼:

RewriteMap location_map txt:/path/to/locations_map.txt 
RewriteCond %{REQUEST_URI} .*search.html.* [NC] 
RewriteCond %{QUERY_STRING} .*location=([^&]+).* [NC] 
RewriteCond ${location_map:%1} ^(.*)$ 
RewriteRule ^(.*)$ /search/%1/ [R=301] 

我怎樣才能換出的參數名稱和丟棄不需要的參數(即上面的參數3)?

回答

1

這將工作,如果它不是不斷變化的參數順序要求。你不能讓訂單保持不變嗎?

RewriteMap location_map txt:/path/to/locations_map.txt 
RewriteCond %{REQUEST_URI} /search.html [NC] 
RewriteCond %{QUERY_STRING} (param1=(.+?)&)?(param2=(.+?)&)?(param3=(.+?)&)?location=(.+?) [NC] 
RewriteRule ^(.*)$ /search/${location_map:%7}/?arg4=%4&arg5=%2 [R=301,last] 

RewriteCond %{REQUEST_URI} /search.html [NC] 
RewriteRule//search-info/ [last]