SEO 优化之 .Net 2.0 下自定义 404 页面
上一篇介绍了在asp.net下自定义404页面并statuscode是404,有网页提醒那个方法需要.net 3.5及以上的环境,因为redirectMode="ResponseRewrite"
是在asp.net 3.5上新加的。
在asp.net 2.0 下我们可以在全局错误处理Application_Error中实现自定义404页面,并返回statuscode:404。
废话少说,代码如下:
protected void Application_Error(object sender, EventArgs e)
{
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config");
System.Web.Configuration.CustomErrorsSection customErrors = (System.Web.Configuration.CustomErrorsSection)config.GetSection("system.web/customErrors");
if (customErrors != null && (customErrors.Mode == System.Web.Configuration.CustomErrorsMode.On || customErrors.Mode == System.Web.Configuration.CustomErrorsMode.RemoteOnly))
{
System.Web.HttpApplication app = (HttpApplication)sender;
System.Exception lastError = app.Server.GetLastError();
System.Web.HttpException httpEx = (HttpException)lastError;
if (httpEx != null)
{
int httpErrorCode = httpEx.GetHttpCode();
string redirect = customErrors.DefaultRedirect;
foreach (System.Web.Configuration.CustomError error in customErrors.Errors)
{
if (error.StatusCode == httpErrorCode) redirect = error.Redirect;
}
app.Server.ClearError();
app.Context.Response.StatusCode = httpErrorCode;
Server.Transfer(redirect);
}
}
}
不过,有一点需要注意一下,在IE和Chrome下,小于512b的自定义错误页面默认会被浏览器自带的错误页面替换掉的:)
Labels:
SEO
SEO 优化之 ASP.NET 自定义错误页面
默认情况下微软已经在 Web.config 加入了如下代码:<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
但对于SEO来说,这样是不可取得,因为这种转向的HTTP Status-Code是302!
302 redirect: 302代表暂时性转移(Temporarily Moved ),在前些年,不少Black Hat SEO曾广泛应用这项技术作弊,目前,各大主要搜索引擎均加强了打击力度,象Google前些年对Business.com以及近来对BMW德国网站的惩罚。即使网站客观上不是spam,也很容易被搜寻引擎容易误判为spam而遭到惩罚。
我们希望自定义ASP.NET 404错误,但又不希望使用ASP.NET默认的302转向的方法,我们要制作两个404页面404b.aspx和404b.htm。
在IIS中指定404错误到静态404b.htm,然后我们还需要修改Web.config:
<customErrors redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/404b.aspx"/>
</customErrors>
我们必须把redirectMode设成"ResponseRewrite",这样ASP.NET才不会用302给我们转向。
RedirectMode 属性设置为 ResponseRedirect,则将用户重定向到该错误页面,并且原始 URL 更改为该错误页面的 URL。
RedirectMode 属性设置为 ResponseRewrite,则将用户定向到错误页面,并且不更改浏览器中的原始 URL。
到这一步还没有完,因为这时候的.aspx等应用程序文件的没有找到资源的错误转向的HTTP Status-Code却变成了200 OK:)
这时候大家应该明白了为什么建2个404页面了吧,404b.aspx页面就是为ASP.NET 404错误准备的,在404b.aspx的Page_Load事件中加入:
Response.Status = "404 Not Found";
整个Page_Load段代码类似:
protected void Page_Load(object sender, EventArgs e)
{
Response.Status = "404 Not Found";
}
这样我们再在我们的站点中随意输入一个网址就能看到我们先前设置的404错误页面了,而且不管是静态文件还是asp.net的动态文件,HTTP Status-Code都是404 Not Found。
Labels:
SEO
ASP.NET 动态 301 重定向
ASP.NET 动态 301 重定向,其实很简单,见代码:protected void Page_Load(object sender, EventArgs e)
{
this.Header.Title = "说说那些事";
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", "http://www.cqun.com");
}
Labels:
SEO
Google Chrome 将可通过登录 Google 帐号同步数据
今天Chromium开发组工程师Tim Steele在开发组论坛里发表文章称,Google浏览器Chrome将新增支持登录Google帐号进行数据同步,同步的内容将首先为用户收藏的"书签",将来会逐步包含其它用户数据,诸如在不同的机器同步历史记录、Cookies以及保存的密码等等。
Tim Steele称目前已经建立了一个库实现了客户端的同步协议,同时Google服务器端的同步支持也已经开发完毕。为了给数以百万计的用户提供同步服务,我们将使用基于Google Talk的XMPP协议来"推送"我们的同步计划,而不是仅仅依靠定期查询更新。
Labels:
Chrome
Subscribe to:
Posts (Atom)
