htaccess用字符替换重写路径中的所有url



我有一个网站,从一个平台转换到另一个。我有成千上万的页面索引,看起来像这样:

domain.com/test/red_widget.html

domain.com/test/big_red_widget.html

domain.com/test/small_nice_red_widget.html

需要像这样:

domain.com/test/red-widget.html

domain.com/test/big-red-widget.html

domain.com/test/small-nice-red-widget.html

所以在这个例子中所有有问题的url都位于"test"路径中,使用下划线并具有html扩展名。

和我需要所有的url在"测试"路径与html扩展被重写为除了下划线,"_",将被替换为破折号,"-"。

我已经尝试了很多东西,但我可以得到兼容我目前的htaccess规则需要继续工作。

现在我有这样的规则:

RewriteRule ^test/(.*).html$ /?kw=$1

可以工作,但是对于像domain.com/test/small_nice_red_widget.html这样的url, kw参数将被设置为small_nice_red_widget,而我真正需要的是将其设置为small-nice-red-widget。

任何指导,如果有人能指出正确的方向,我将不胜感激。

这应该可以为您工作:

RewriteRule ^test/(.*)_(.*)_(.*)_(.*).html$ /test/$1-$2-$3-$4.html [R,L]
RewriteRule ^test/(.*)_(.*)_(.*).html$ /test/$1-$2-$3.html [R,L]
RewriteRule ^test/(.*)_(.*).html$ /test/$1-$2.html [R,L]

您也可以这样做,将任意数量的下划线替换为斜杠:

RewriteRule ^test/(.*)_(.*).html$ /test/$1-$2.html [L,E=DASH:Y]
RewriteCond %{ENV:DASH} Y
RewriteRule ^([^_]+)$ /$1 [L,R=301]

最新更新