Htaccess重写名称/值对

时间:2013-01-07 13:26:59

标签: apache .htaccess mod-rewrite url-rewriting

我需要一些帮助来编写一个动态规则,我可以添加以'/'分隔的名称/值对,而不是name1 = value& name2 = value2

e.g

http://www.example.com/jeans.html?color=24&manufacturer=3 http://www.example.com/jeans/color/black/manufacturer/jonh-miller.html

&安培;

http://www.example.com/jeans.html?color=24&manufacturer=3&size=1 http://www.example.com/jeans/color/black/manufacturer/jonh-miller/size/xl.html

等等。 任何人都能指出一些好的文件吗?

由于

1 个答案:

答案 0 :(得分:1)

使用RewriteMap

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritemap

您应首先匹配参数然后使用地图,例如:

RewriteMap my_redir_map1 txt:map_rewrite.txt
RewriteMap my_redir_map2 txt:map_rewrite.txt

RewriteCond %{QUERY_STRING} ^.*(\bcolor\b=(\w+|&\w+;)+)
RewriteCond %{QUERY_STRING} ^.*(\bmanufacturer\b=(\w+|&\w+;)+)
RewriteRule ^/([^/\.]+).html /$1/color/${my_redir_map1:%1}/manufacturer/${my_redir_map2:%2}.html  [L]

这应符合案例:

http://www.example.com/jeans.html?color=24&manufacturer=3

并将其重写为:

http://www.example.com/jeans/color/black/manufacturer/jonh-miller.html

请注意:此解决方案需要每个参数的映射以及每个最终路径的重写。