asp.net - 错误 404 的默认重定向

标签 asp.net http redirect default

我想在我的 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 错误页面的方式:

<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>

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

您可以使用 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>

关于asp.net - 错误 404 的默认重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4483849/

相关文章:

javascript - 如何在提交表单aspx后调用函数

java - 使用 BufferedInputStream 读取套接字

javascript - 使用 Javascript 在内容配置 header 中设置大小参数

python - 如何在 scrapy 中获取原始 start_url(在重定向之前)

使用 web.config 重定向排除 robots.txt

php - 检索 twitter 之类的用户名链接 php

javascript - javascript src url 中的动态值

asp.net - 为什么在使用当前同步上下文启动任务时未设置 ASP.NET HttpContext.Current

asp.net - OWIN 多应用不记名 token 认证

.htaccess - 在 htaccess 中忽略来自 https 的完整子域