【发布时间】:2015-01-22 04:40:25
【问题描述】:
我在 Htaccess 中有如下动态 URL 的重写规则
RewriteRule ^cartoon-([^-]*)-([^-]*)\.html$ /fm
/cart_new?r_id=$1&location=$2 [L]
此规则生成的 URL 为 http://localhost/fm/cartoon-34-singapore.html
现在我的客户想将此 URL 更改为 http://localhost/fm/singapore/34/goofie,我将 .htaccess 写为
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /fm
/cart_new?location=$1&r_id=$2&cartooname=$3 [L]
上述重写工作正常,但客户希望所有旧 URL(例如http://localhost/fm/cartoon-34-singapore.html)应 301 重定向到http://localhost/fm/singapore/34/goofie。
这快把我逼疯了。我尝试了各种方法,但似乎都没有。
【问题讨论】:
-
旧 URL
http://localhost/fm/cartoon-34-singapore.html中没有cartooname。所以它不能重定向到http://localhost/fm/singapore/34/goofie -
如果 URL 模式匹配,我们能否通过发送 301 rediredct 标头以某种方式在 PHP 中更改 URL?
-
是的,它可以在 PHP 中完成,然后在 PHP 本身中进行重定向
-
您能否将其添加为答案,以便我接受。
标签: .htaccess mod-rewrite redirect