mod重写规则替换+ with -

时间:2011-07-09 09:10:56

标签: regex mod-rewrite

我有一个重写规则,用连字符替换下划线:

RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]

RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]

此规则很棒,因为它会递归检查,因此会将/word1_word2/变为/word1-word2/

但也适用/word1_word2_word3_word4//word1-word2-word3-word4/

如何修改此项以将+符号替换为 - (连字符)?

/word1+word2/变成/word1-word2/

/word1+word2+word3+word4/变成/word1-word2-word3-word4/

我已经尝试了十几种变体,似乎无法使其发挥作用。

1 个答案:

答案 0 :(得分:1)

只需在您拥有下划线的地方添加角色:

RewriteRule ^([^_+]*)[_+]([^_+]*[_+].*) $1-$2 [N]

RewriteRule ^([^_+]*)[_+]([^_+]*)$ /$1-$2 [L,R=301]