BlogEngine.net提供了一个叫做SearchOnSearch的控件,其作用是当用户通过搜索引擎访问网站时,将用户使用的搜索关键字转到BlogEngine.net的站内搜索引擎再搜索一次,并将搜索结果显示给用户。SearchOnSearch将搜索引擎和站内搜索结合在一起,为用户提供更准确、详细的搜索结果,是一个相当不错的控件。
可惜的是SearchOnSearch只支持Google搜索,不支持百度搜索。原因其实很简单,Google搜索时其关键字在URL中是以"q="开头的,百度是以"wd="开头的,而SearchOnSearch控件所使用的拆分搜索关键字的正则表达式只支持以"q=" 开头的关键字,如下所示:
static SearchOnSearch()
{
// Matches the query string parameter "q" and its value. Does not match if "q" is blank.
_rxSearchTerm = new Regex("[?&]q=([^&#]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
将其改为
static SearchOnSearch()
{
// Matches the query string parameter "q" and its value. Does not match if "q" is blank.
_rxSearchTerm = new Regex("[?&](q|wd)=([^&#]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
另外需要注意百度是以GB2312格式对搜索关键字进行编码的,而Google则是以UTF-8进行编码的,因此需要先获取UrlReferrer的OriginalString,再根据不同的搜索引擎按GB2312或者UTF-8对搜索关键字解码,这需要修改Html函数和GetSearchTerm函数,如下所示:
/// <summary>
/// Checks the referrer to see if it qualifies as a search.
/// </summary>
private string Html()
{
if (Context.Request.UrlReferrer != null && !Context.Request.UrlReferrer.ToString().Contains(Utils.AbsoluteWebRoot.ToString()) && IsSearch)
{
//string referrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLowerInvariant();
//Using OriginalString to decode.
//Edited by Jason,2008-08-22
string referrer = HttpContext.Current.Request.UrlReferrer.OriginalString.ToLowerInvariant();
string searchTerm = GetSearchTerm(referrer);
List<IPublishable> items = Search.Hits(searchTerm, false);
if (items.Count == 0)
return null;
return WriteHtml(items, searchTerm);
}
return null;
}
/// <summary>
/// Retrieves the search term from the specified referrer string.
/// </summary>
//Edited by jason
//2008-08-22
private string GetSearchTerm(string referrer)
{
string term = string.Empty;
Match match = _rxSearchTerm.Match(referrer);
if (match.Success && match.Groups.Count>2)
{
//baidu.com is using GB2312 encoding,and Google is using UTF8 encoding
//Edited by jason
//2008-08-22
if (match.Groups[1].Value == "wd")
term=System.Web.HttpUtility.UrlDecode(match.Groups[2].Value, System.Text.UnicodeEncoding.GetEncoding("GB2312"));
else
term = System.Web.HttpUtility.UrlDecode(match.Groups[2].Value, System.Text.UnicodeEncoding.UTF8);
}
return term.Replace("+", " ");
}
现在,BlogEngine.net就同时支持Google搜索和百度搜索了。下面是来自百度的搜索结果:
附:SearchOnSeach.cs源代码,下载后替换/App_Code/Controls/下的同名文件即可
SearchOnSearch.rar (1.67 kb)