错误 404 的默认重定向 [英] Default redirect for Error 404

查看:45
本文介绍了错误 404 的默认重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 ASP.net 网站中引入一项功能,每当收到对我域中未知 URL 的请求时,用户都会被重定向到我的根目录中的 error_404.htm 页面应用程序.

例如,如果请求是 http://www.mydomain.com/blahblahblah

然后不是返回标准的 404 错误页面,我希望它将请求重定向到 http://www.mydomain.com/error_404.htm

更新IIS 7.5 版和 .NET Framework 4 版

更新/blah.aspx 重定向但 /blah 没有

解决方案

这是为 ASP.NET 和非 ASP.NET 请求配置自定义 404 错误页面的方式:

<预><代码><配置><system.web><编译targetFramework="4.0"/><customErrors mode="On" redirectMode="ResponseRewrite"><error statusCode="404" redirect="http404.aspx"/></customErrors></system.web><system.webServer><httpErrors errorMode="自定义"><remove statusCode="404"/><error statusCode="404" path="/http404.aspx" responseMode="ExecuteURL"/></httpErrors></system.webServer></配置>

正如其他人已经指出的那样,您不应该使用 HTTP 重定向将用户发送到主页,这不仅会使用户感到困惑,还会使机器(例如搜索引擎)感到困惑.使用 404 状态代码而不是 3xx 代码很重要.

您可以使用 HTML 上的元刷新来实现所需的功能:

<%@ Page Language="C#" %><html xmlns="http://www.w3.org/1999/xhtml"><头><title>未找到</title><meta http-equiv="refresh" content="5;url=/"/><身体><h1>未找到</h1><p>重定向到主页...</p>

I want to introduce a functionality in my ASP.net website that, whenever a request is received for an unknown URL on my domain, the user is redirected to my error_404.htm page in the root of the application.

For example, if the request is http://www.mydomain.com/blahblahblah

Then instead of returning the standard 404 error page, I want it to redirect the request to http://www.mydomain.com/error_404.htm

Update IIS Version 7.5 and .NET Framework Version 4

Update /blah.aspx redirects but /blah does not

解决方案

This is how you configure a custom 404 error page for both ASP.NET and non-ASP.NET requests:

<configuration>

   <system.web>
      <compilation targetFramework="4.0" />

      <customErrors mode="On" redirectMode="ResponseRewrite">
         <error statusCode="404" redirect="http404.aspx" />
      </customErrors>
   </system.web>

   <system.webServer>
      <httpErrors errorMode="Custom">
         <remove statusCode="404"/>
         <error statusCode="404" path="/http404.aspx" responseMode="ExecuteURL"/>
      </httpErrors>
   </system.webServer>

</configuration>

As others already pointed out, you should not use an HTTP redirection to send the user to the home page, this is not only confusing to users but also to machines (e.g. search engines). It is important to use the 404 status code and not a 3xx code.

You can achieve the desired functionality using meta refresh on HTML:

<%@ Page Language="C#" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Not Found</title>
   <meta http-equiv="refresh" content="5;url=/"/>
</head>
<body>
   <h1>Not Found</h1>
   <p>Redirecting to Home...</p>
</body>
</html>

这篇关于错误 404 的默认重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆