完善SearchOnSearch,支持Baidu和yahoo

by benben 5/18/2008 6:11:07 AM

修改BlogEngine默认404页面,更利于SEO里,我们讲了修改404页面,这次我们对SearchOnSearch开刀。SearchOnSearch的描述为:If the visitor arrives through a search engine, this control will display an in-site search result based on the same search term.但我们从搜索引擎进入blog时,会自动获取关键字并列出文章里相似的文章,Wordpress也有类似插件,而我们的BlogEngine内部就支持。哈哈~

文件位于/App_Code/Controls/SearchOnSearch.cs。先来修改IsSearch,修改后代码为

    private string IsSearch
    {
      get
      {
        string referrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLowerInvariant();
        if (referrer.Contains("google.") && referrer.Contains("q="))
            return "q=";
        //从Google跑过来的
        if (referrer.Contains("yahoo.") && referrer.Contains("p="))
            return "p=";
       //你是从yahoo跑过来的
        if (referrer.Contains("wd="))
            return "wd=";
       //你是从百度跑过来的
        if (referrer.Contains("q="))
            return "q=";
       //你是无名氏
        return string.Empty;
      }
    }

然后修改GetSearchTerm()这个方法,修改后代码为

    private string GetSearchTerm(string referrer,string key)
    {
      int start = referrer.IndexOf(key) + key.Length;
      int stop = referrer.IndexOf("&", start);
      if (stop == -1)
        stop = referrer.Length;

      string term = referrer.Substring(start, stop - start);
      return term.Replace("+", " ");
    }

然后再修改Html()里对应的方法,IsSearch原来返回类型为bool,现在返回string,所以改为IsSearch.Length>0,GetSearchTerm()增加了一个传入值,改为GetSearchTerm(referrer,IsSearch )然后把文件更新到对应目录即可。

以下是从百度搜索119797进入blog后的抓图。

如果你懒得自己动手,直接下载俺修改好的文件,覆盖到对应目录即可。 猛击下载SearchOnSearch.zip

test

Tags: , ,

.net

Add comment

 

   

 

  Country flag

buquote
Loading

Powered by BlogEngine.NET 苏ICP备08023017号



Recent comments

Author

声明

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in