【问题标题】:Htaccess 301 redirect htaccess or phpHtaccess 301 重定向 htaccess 或 php
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

  • 像魅力一样工作:)
猜你喜欢
  • 1970-01-01
  • 2012-11-25
  • 2013-06-30
  • 2011-05-01
  • 2011-01-03
  • 2012-03-10
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多