【问题标题】:How can I swap URL parameters and discarding others with mod_rewrite?如何使用 mod_rewrite 交换 URL 参数并丢弃其他参数?
【发布时间】:2011-06-28 06:21:16
【问题描述】:

我需要重定向简化版:

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

收件人:

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

参数可以是任意顺序或缺失,位置是地图文件中扩展的代码(英国、英国等)。

如果没有匹配的参数重定向到:

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

如何换掉参数名称并丢弃不需要的参数(即上面的 param3)?

【问题讨论】:

    标签: regex apache url mod-rewrite configuration


    【解决方案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]
    

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 2018-10-14
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      相关资源
      最近更新 更多