drj14664 2013-02-20 02:03
浏览 23
已采纳

htaccess - 如果链接不存在,则重定向到搜索

I have recently changed my site permalink. Some links are indexed by google and they are in search result. but due to changing of my permalink, now google search send them to error 404 page and that is really bad.

So, instead of showing error404 i want to redirect them to search page extracting keyword from link, for example:

my original link which does not exist now:

http://www.mydomain.com/2013/01/some-title-of-my-site/

Now if it is error404 rewrite them to search page

http://www.mydomain.com/search?q=some+title+of+my+site

Here is my solution: it is working and awesome (no error page :-))

using htaccess to redirect error404 to search page and then set search query by explode url

$urlfrom404 =   strtolower($_SERVER['REQUEST_URI']);
$tokens = explode('/', $urlfrom404);
$removeHyphen = strtr($tokens[sizeof($tokens)-2], '-', ' ');
  • 写回答

1条回答 默认 最新

  • dongxi8297 2013-02-20 02:15
    关注

    You don't need a .htaccess to do the redirect. Instead you can do this way:

    if ( num_rows_result ( $query ) == 0 )
        header ( 'Location: /search?q=' . $_GET['post'] );
    

    The function num_rows_result ( $query ) should return the number of rows that matched from your DB, and it should be either 0 or 1 and nothing else, as you are supposed to compare it with a Primary Key.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化