mod rewrite rule to replace + with -
I have a rewrite rule which replaces underscores with hyphens:
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
This rule is great because it will check recursively so it turns /word1_word2/
into /word1-word2/
But also works for /word1_word2_word3_word4/
into /word1-wor开发者_如何学Pythond2-word3-word4/
How can I modify this to replace the + symbol with a - (hyphen)?
So that /word1+word2/
turns into /word1-word2/
and
/word1+word2+word3+word4/
turns into /word1-word2-word3-word4/
I've tried a dozen variations and cant seem to get this to work.
Just add the character everywhere where you have an underscore:
RewriteRule ^([^_+]*)[_+]([^_+]*[_+].*) $1-$2 [N]
RewriteRule ^([^_+]*)[_+]([^_+]*)$ /$1-$2 [L,R=301]
精彩评论