2011-02-16 63 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]