返回404错误,转自(且歌且行)

转自-http://www.cnblogs.com/WuYisLLQ/archive/2008/12/25/1362165.html

HTTP404对SEO的影响

  自定义404错误页面是提供用户体验的很好的做法,但在应用过程中往往并未注意到对搜索引擎的影响,譬如:错误的服务器端配置导致返回“200”状态码或自定义404错误页面使用Meta Refresh导致返回“302”状态码。正确设置的自定义404错误页面,不仅应当能够正确地显示,同时,应该返回“404”错误代码,而不是“200”或“302”。虽然对访问的用户而言,HTTP状态码究竟是“404”还是“200”来说并没有什么区别,但对搜索引擎而言,这则是相当重要的。

使用web.config中的customErrors来做的话并不好,因为asp.net会在返回的head头上加上302状态,即使你在自定义的404页面上设置Response.StatusCode = 404也是无法消除asp.net加的302错误。因此我的办法就是在全局错误处理Application_Error中设置并返回html代码。

代码:

 void Application_Error(object sender, EventArgs e)
    {
        // 在出现未处理的错误时运行的代码
        Response.Clear();
        Response.StatusCode = 404;
        Response.Write("<html xmlns=\"http://www.w3.org/1999 /xhtml/><head><meta http-equiv=\"Content-Type\" content= \"text/html; charset=utf-8\" /><title>页面没有找到</title><meta http-equiv='refresh' content='4;url=http://www.bigbigwatch.com'>"
       + "<script type=\"text/javascript\">"
       + "function daoshu()"
       + "{ var djs = document.getElementById(\"daojishi\");"
       + "if(djs.innerHTML == 0){window.location.href='http://www.bigbigwatch.com';"
       + "return false;"
       + "}"
       + "djs.innerHTML = djs.innerHTML - 1;}"
       + "window.setInterval(\"daoshu()\", 1000);"
       + "<" + "/script>"
       + "</head><body><form id=\"form1\" runat=\"server\"><div style=\"margin-left: 100;\"> The error may occur:<ul><li>You find the page does not exist; </li><li>Your input is wrong; </li></ul><span id=\"daojishi\" style=\"color:Blue;font-size:x-large;\">5</span> seconds after their Jump to Home page, or if your browserdoes not support Jump, please <a href='http://www.bigbigwatch.com'>click here</a> Jump!</div></form></body></html>"
      );
        Response.End();
    }

ie有个BUG:如果404页面大小不够521b的话会被ie的默认内容替换。

另个需要注意的是meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />这个必须在title标签前,否则ie6下不能跳转。

转载于:https://www.cnblogs.com/wyxy2005/archive/2009/01/07/1370814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值