Ajax 网站的搜索引擎优化
如何在 Ajax 网站中实现搜索引擎优化
引言
搜索引擎爬虫不会遵循 JavaScript 链接。如果您使用 AJAX 动态加载文本,搜索引擎将不会索引它。URL 重写是可用于让爬虫索引网站上所有链接的方法。
目标
- 如果普通用户访问网站,他应该看到启用了 AJAX (JavaScript) 链接的网站。
- 如果爬虫访问网站,链接应该是可供索引的锚链接。
- 如果爬虫索引的锚链接被浏览,那么动态内容应该可见,同时保持 URL 不变。
例如:如果用户浏览网站 http://example.com 并点击“第一篇文章”链接,则第一篇文章的内容会动态加载。但如果搜索引擎爬虫浏览网站,链接将是 http://example.com/Articles/FirstArticle,这是可供爬取的。现在,如果普通用户请求此链接(已由搜索引擎索引),那么“第一篇文章”应该显示,同时 URL 保持为 http://example.com。
Using the Code
URL 重写
- 在 HTML 中将锚标签编写为
<a href=”http://www.example.com/Aditya/Bhave/”>Aditya Bhave</a>
。 - 当用户点击此链接时,浏览器将发送一个带有上述 URL 的请求。
- 此 URL 将由自定义
HttpModule
处理。 HttpModule
会将上述 URL 转换为应用程序所需的 URL。即,http://www.example.com/alllinks.aspx?firstname=Aditya&lastname=Bhave- 请求被处理,就像请求是 http://www.example.com/alllinks.aspx?firstname=Aditya&lastname=Bhave 一样。
- 响应被发送到浏览器,但 URL 仍然是 http://www.example.com/alllinks/Aditya/Bhave/。
您可以将此与 ASP.NET 中的 Server.Transfer(“/…./…”)
进行比较。
重写 URL 的 HttpModule 简单示例
public class SimpleRewriter : IHttpModule
{
HttpApplication _application = null;
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new System.EventHandler(context_BeginRequest);
_application = context;
}
void context_BeginRequest(object sender, EventArgs e)
{
try
{
string requestUrl = _application.Context.Request.Path.Substring
(0, _application.Context.Request.Path.LastIndexOf("/"));
string[] parameters = requestUrl.Split(new char[] { '/' });
if (parameters.Length > 2)
{
int paramLength = parameters.Length;
string firstname = parameters[paramLength - 2];
string lastname = parameters[paramLength - 1];
_application.Context.RewritePath(string.Format
("~/alllinks.aspx?firstname={0}&lastname={1}", firstname, lastname));
}
}
catch (Exception ee)
{
//Redirect to error page
//Or throw custom exception
}
}
#endregion
}
URL 重写作为 AJAX 网站的搜索引擎优化技术
- AJAX 网站具有所有 JavaScript 链接。
- 如果我们使用
LinkButton
,则链接将是<a href=”javascript:__doPostback(‘….’,’…..’);”>Aditya Bhave</a>
在浏览器中渲染。
- 如果爬虫遇到 JavaScript 链接,它会被跳过(不被索引)。
- 如果我们希望我们的链接:Aditya Bhave 被爬虫索引并在搜索引擎中搜索,那么链接应该在锚标签中,例如:
<a href=”http://www.example.com/Aditya/Bhave/”>Aditya Bhave</a>
- 大多数爬虫不支持 JavaScript。当爬虫访问网站时,会向请求发送用户代理,告诉我们这是爬虫而不是普通用户。我们可以检查浏览器是否支持 JavaScript。如果它不支持 JavaScript,那么我们应该隐藏
LinkButton
并在运行时添加锚标签。所以应该有两个链接。<asp:LinkButton ID="lnkLink" runat="server" Text='Some Text' OnClick="lnkLink_Click"></asp:LinkButton>
和
<a href="" runat="server" id="htmlLnkLink"></a>
- 如果浏览器不支持 JavaScript,则隐藏
LinkButton
并更改锚链接的href
属性,使其可见。
示例
if (Request.Browser.EcmaScriptVersion.Major <= 0) // Checks if browser supports JS
{
lnkArticle.Visible = false;
aLnkArticle.attributes[“href”]=
"http://www.example.com/alllinks.aspx/Firstname/Lastname;
aLnkArticle.InnerText = “FirstName LastName”; //To show the link text
aLnkArticle.Visible = true;
}
当爬虫访问锚链接,即 http://www.example.com/alllinks.aspx/Aditya/Bhave/ 时,使用 URL 重写将 URL 更改为 http://www.example.com/alllinks.aspx?firstname=Aditya&lastname=Bhave。
在 alllinks.aspx 页面的 Page_Load
事件中,检查是否存在名为 firstname
或 lastname
的 Querystring
变量。
如果存在,则执行 LinkButton_Click
事件中的代码,以便向爬虫显示内容。
string firstname = Request.QueryString["firstname"];
string lastname = Request.QueryString["lastname"];
SetName(firstname, lastname);
现在爬虫将看到内容并索引您的 URL,即 http://www.example.com/Aditya/Bhave/ ,它将出现在搜索结果中。
现在,如果普通用户点击上述链接(在搜索结果中找到的),他必须看到动态内容,同时保持 URL 与 http://www.example.com/alllinks.aspx 相同。
我们可以实现以下方法来实现这一点。
- URL http://www.example.com/Aditya/Bhave/ 将被
HttpModule
重写为:
http://www.example.com?firstname=Aditya&lastname=Bhave - 检查 querystring 是否存在以及浏览器是否支持 JavaScript。如果两者都为
true
,则将firstname
和lastna
me 放入Session
变量并重定向到同一页面。 - 现在 querystring 已消失,但
Session
变量存在。检查Session
变量是否存在,然后执行与LinkButton_Click
中相同的代码以显示动态内容并删除Session
变量。
#region Process RequestParameters
if (Request.QueryString["firstname"] != null)
{
if (Request.Browser.EcmaScriptVersion.Major > 0)
{
//Normal user
Session["firstname"] = null;
Session["lastname"] = null;
Session["firstname"] = Request.QueryString["firstname"];
Session["lastname"] = Request.QueryString["lastname"];
Response.Redirect("alllinks.aspx", true);
}
else
{
//Crawler : Show Dynamic Content.
string firstname = Request.QueryString["firstname"];
string lastname = Request.QueryString["lastname"];
SetName(firstname, lastname);
}
}
#endregion
#region Process Session Variables
if (Session["firstname"] != null)
{
//Normal user : show dynamic content and remove session variables.
string firstname = Convert.ToString(Session["firstname"]);
string lastname = Convert.ToString(Session["lastname"]);
SetName(firstname, lastname);
Session.Remove("firstname");
Session.Remove("lastname");
}
#endregion
请参阅附件的解决方案。
如何检查是否正常工作
使用 Mozilla Firefox 的附加组件“PrefBar
”。选择用户代理为 Lynx。(这将像浏览器是 Lynx 一样浏览页面。Lynx 是一个不支持 JavaScript 的文本浏览器。)
禁用颜色、图像、JavaScript、Flash 并浏览网站。在 Internet Explorer 中浏览同一网站。比较这两个网站。在 Mozilla (Lynx) 中,链接是普通的锚链接,如果您点击它,则执行完整的回发。但如果您点击 Internet Explorer 中的链接,则使用 AJAX 并动态加载内容。从 Firefox 复制链接,例如 https://:1234/alllinks.aspx/Aditya/Bhave/ 并在 Internet Explorer 中浏览它。在 Internet Explorer 中,它应显示 URL 为 https://:1234/alllinks.aspx ,同时加载动态内容。
非常欢迎您的建议。感谢阅读。
历史
- 2010 年 8 月 11 日:初始发布