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 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)