Mod_Rewrite问题

时间:2010-12-19 09:02:18

标签: mod-rewrite

如何使用mod_rewrite将以下URL示例1转换为示例2?

示例1

http://www.example.com/cat/index.php?cat=fruit&sub1=apple&sub2=green

示例2

http://www.example.com/cat/fruit/apple/green/

1 个答案:

答案 0 :(得分:1)

使用类似的东西:

RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ index.php?$1=$2&sub1=$3&sub2=$4 [NC]

$1$2$3$4代表四个匹配的群组(.*)

如果存在可变数量的子类别,则应更改策略:

RewriteRule ^(.*)/(.*)/(.*)/$ index.php?$1=$2&subcat_string=$3 [NC]

手动解析subcat_string(python中的示例):

# subcat_string will be something like "apple/green"
subcats = subcat_string.split("/")
# subcats is now ["apple", "green"]