开发者

How can I swap URL parameters and discarding others with mod_rewrite?

开发者 https://www.devze.com 2023-02-11 23:05 出处:网络
I need to redirect the simplified: /search.ht开发者_开发百科ml?param1=val1&param2=val2&param3=val3&location=UK

I need to redirect the simplified:

/search.ht开发者_开发百科ml?param1=val1&param2=val2&param3=val3&location=UK

To:

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

Parameters may be in any order or missing, location is a code with expansions in a map file (UK United Kingdom, etc).

If there are no matching parameters redirect to:

/search-info/

Current code for location expansion:

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]

How can I swap out the parameter names and discard parameters that aren't needed (i.e. param3 above)?


This would work if it weren't the changing parameter order requirement. Can't you make the order constant?

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]
0

精彩评论

暂无评论...
验证码 换一张
取 消