Htaccess recursively replace underscores with hyphens

前端 未结 2 872
醉酒成梦
醉酒成梦 2021-01-07 03:59

I\'m trying to redirect all URLs with underscores to the same URL but with underscores replaced by hyphens.

I\'ve tried many examples online but these either provide

2条回答
  •  滥情空心
    2021-01-07 04:50

    You can use that:

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

    After @anubhava explanation. If big number of underscores can be a problem. You can use that (without variables):

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

提交回复
热议问题