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 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的