Htaccess 301 重定向 htaccess 或 php

Htaccess 301 redirect htaccess or php

我在 Htaccess 中有一个重写规则,如下所示用于动态 URL

RewriteRule ^cartoon-([^-]*)-([^-]*)\.html$ /fm
/cart_new?r_id=&location= [L]

此规则导致 URL 为 http://localhost/fm/cartoon-34-singapore.html

现在我的客户想将此 URL 更改为 http://localhost/fm/singapore/34/goofie,我将 .htaccess 写为

RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /fm
/cart_new?location=&r_id=&cartooname= [L]

上面的重写工作正常,但客户希望所有旧的 URLs,如 http://localhost/fm/cartoon-34-singapore.html 应 301 重定向到 http://localhost/fm/singapore/34/goofie.

这让我发疯。我尝试了各种方法,但 none 似乎有效。

在您的 PHP 代码中,您可以执行类似这样的重定向操作:

header ('HTTP/1.1 301 Moved Permanently');
header("Location: {$rebuilt_url}");

此处$rebuilt_ur将通过查询您的数据库设置为http://localhost/fm/singapore/34/goofie