htaccess 301 重定向 htaccess 或 php

2024-04-06

我在 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 像 i.e.http://localhost/fm/cartoon-34-singapore.html应将 301 重定向到http://localhost/fm/singapore/34/goofie.

这真让我抓狂。我尝试过各种方法,但似乎没有一个有效。


在 PHP 代码中,您可以执行以下操作来进行重定向:

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

htaccess 301 重定向 htaccess 或 php 的相关文章

随机推荐