我在htaccess文件中使用以下代码
Redirect 301 /([0-9]{1,2})+_([A-Za-z])$ /brand/$1-$2
重定向以下类型的网址格式:
www.mywebsite.com/{ID}_{BrandName}
所以基本上我必须重定向
www.mywebsite.com/{ID}_{BrandName} to www.mywebsite.com/brand/{ID}-{BrandName}
。
例如:将301重定向到www.mywebsite.com/5_TestBrand to www.mywebsite.com/brand/5-TestBrand
。
请建议。
以下是无效的htaccess代码:
Redirect 301 /([0-9]{1,2})+_([A-Za-z])$ /brand/$1-$2
请指南。
谢谢!
答案 0 :(得分:3)
您无法在Redirect
指令中使用正则表达式和捕获的组。请改用RediectMatch
:
RedirectMatch 301 ^/([0-9]+)_([A-Za-z]+)/?$ /brand/$1-$2
答案 1 :(得分:0)
确保重写引擎位于htaccess的顶部。
RewriteEngine on
然后,不应该看起来像这样:
RewriteEngine on
RewriteRule ^(.*)/([0-9]+)_([A-Za-z])$ /$1/brand/$2-$3 [R=301,NC,NE,L]
或者也许:
RewriteEngine on
RewriteRule ^([0-9]+)_([A-Za-z])$ /brand/$1-$2 [R=301,NC,NE,L]
类似的东西,我总是不得不用htaccess来完善它。