为什么在 ASP.NET 中使用自定义 404 页面时,不存在的页面会返回 302 状态

发布于 2024-09-01 19:55:53 字数 250 浏览 3 评论 0原文

我已经设置了一个自定义 404 页面 custom404.aspx,该页面正确返回“404 Not Found”错误,但是最初请求的不存在页面返回“302 Found”状态。

因此,当我测试 thispagedoesnotexist.aspx 时,它返回“302 Found”,然后 custom404.aspx 加载并返回“404 Not Found”状态。

我想确保搜索蜘蛛/机器人了解所请求的页面不存在并且不应显示在任何搜索结果中。这个设置正确吗?

I have setup a custom 404 page custom404.aspx that returns a "404 Not Found" error correctly, however the non-existant page that was initially requested returns a "302 Found" status.

So when I test thispagedoesnotexist.aspx, it returns a "302 Found" then the custom404.aspx loads and returns a "404 Not Found" status.

I want to make sure that search spiders/bots understand that the requested page does not exist and should not show up in any search results. Is this setup properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀子花开つ 2024-09-08 19:55:53

为了回答上述问题,这就是自定义错误在 ASP.NET 中的工作原理。我见过一些例子(虽然我手头没有),人们使用 HttpModule 来拦截错误,并向看起来不是机器人的用户提供自定义错误页面,并向看起来像是机器人的用户提供 404 响应。成为机器人。

从另一个方向来看,我不确定机器人是否会定期遵循 302 响应。也许更相关的是,让您的错误页面(假设它可用)出现在搜索结果中真的那么糟糕吗?至少用户有一个到您网站的链接——这比让您的竞争对手网站出现要好......

To answer the stated question, that's just how the custom errors work in ASP.NET. I've seen examples (though I don't have one handy) where people have used HttpModules to intercept errors, and serve a custom error page to users that don't appear to be bots, and a 404 response to users that appear to be bots.

In a different direction, I'm not sure bots regularly follow 302 responses anyway. More relevant perhaps, is it really so bad to have your error page (assuming it's usable) show up in search results? At least the user has a link to your site--that's better than having your competitors site show up...

葬心 2024-09-08 19:55:53

实际上,它很可能完全按照 web.config 中的配置运行。

取决于您使用的 Web 服务器 (IIS 6/7) 以及您用来配置 404 自定义页面的方式,但基本上这就是重定向到自定义错误页面和执行它之间的区别!

目前您已将 asp.net/IIS 配置为重定向到错误页面。更改设置来执行该页面,您将得到您正在寻找的内容:)

Actually, it's most likely working exactly as it's configured in your web.config.

Depending on what web server you use (IIS 6/7) and which way you used to configure the 404 custom page, but basically this is the difference between redirecting to the custom error page, and executing it!

Currently you have configured the asp.net / IIS to redirect to the error page. Change the settings to execute the page and you'll get exactly what you're looking for :)

悍妇囚夫 2024-09-08 19:55:53

我找到了一个解决方案:

在你的 web.config 中放置 "redirectMode=ResponseRewrite":

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/errors/GeneralError.aspx">
            <error statusCode="404" redirect="~/errors/PageNotFound.aspx" />
        </customErrors>

... 在 Page_Load 中的自定义 404 页面中放置: this.Response.Status = "404 Not Found";

瞧!

I have found a solution for that:

in your web.config put "redirectMode=ResponseRewrite":

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/errors/GeneralError.aspx">
            <error statusCode="404" redirect="~/errors/PageNotFound.aspx" />
        </customErrors>

... And in your custom 404 page in Page_Load put : this.Response.Status = "404 Not Found";

Voila!

像极了他 2024-09-08 19:55:53

简单地说...

例如,使用:

/path_to_error_page.html 

...而不是...

http://www .example.com/path_to_error_page.html

原因是服务器解释初始请求,然后生成到 404 的重定向,以便您的客户端有效地获得 2 个响应。

相对路径不会产生重定向,但内部服务器传输只会产生 1 个响应,即您想要的响应!

这应该可以解决你的问题。

Simply put ...

As an example, use:

/path_to_error_page.html 

... and not ...

http://www.example.com/path_to_error_page.html

The reason being that the server interprets the initial request then generates a redirect to the 404 so your client effectively gets 2 responses.

Relative paths don't produce a redirect but an internal server transfer this results in only 1 response, the one you want !!

This should fix your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文