URL重定向方法


URL redirect methods

我正在使用查询从DB获取行。在vendor.php我有查询,搜索DB的id。结束链接看起来像这样:

www.example.com/vendor?id=3

现在,我想把这个页面的逻辑改为按标题搜索DB

www.example.com/vendor?title=hi-you

当然,在我的数据库中,id=3title="hi-you"是同一行

,然后使用.htaccess制作一个漂亮的url:

RewriteRule ^vendor/?$ vendor.php    [NC,L]
RewriteRule ^vendor/([A-Za-z0-9-]+)/?$ vendor.php?vendor_title=$1 [NC,L]

我该如何处理旧方法链接?如何重定向它们?

像这样在vendor.php的顶部应该可以工作:

<?php
   $id = $_GET['id'];
   if (!empty($id)) {
      $dbTitle = // get title by doing a database query using $id
      // redirect with 301 to correct /<id>/<title> page
      header ('HTTP/1.1 301 Moved Permanently');
      header('Location: /vendor/' . $dbTitle);
      exit;
   }
   // rest of your script
?>