为 IIS7 网站配置带有重定向的自定义 ASP 404 页面 [英] Configuring custom ASP 404 page with redirect for IIS7 website

查看:30
本文介绍了为 IIS7 网站配置带有重定向的自定义 ASP 404 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将现有网站从 IIS6 迁移到 IIS7,但在设置 404 错误页面时遇到了一些困难.我们的 404 错误页面是这样工作的:

  • 自定义 ASP 页面根据特殊"网址的短列表(例如 http://example.com/limited-offers).
  • 如果 URL 已知,则重定向到该页面的实际 URL.
  • 否则访问者会被重定向到一个带有 404 状态码的静态错误页面.

在 IIS6 中,这如宣传的那样工作,但在 IIS7 中,有些事情发生了变化.IIS7 在遇到定义了错误页面的状态代码时,将始终显示配置的错误页面.如果我们的静态错误页面带有 404-statuscode,这意味着 IIS7 将再次执行自定义 ASP 页面.这会导致无限重定向.

我们发现可以通过在 Web.Config 中添加设置来规避此行为

<httpErrors existingResponse="PassThrough"/></system.webServer>

但是,在添加此之后,我们的自定义 ASP 页面拒绝重定向.在与 Fiddler 核实后,IIS7 似乎强制使用 404 状态码,覆盖了我们的 302 重定向.

谁能推荐另一种方法来解决我们的问题?

解决方案

我成功地使用了一个类似的设置,我从 IIS 6 迁移到了 IIS 7.我的 web.config 有以下部分;

<块引用>

<httpErrors errorMode="自定义"><remove statusCode="500" subStatusCode="-1"/><remove statusCode="404" subStatusCode="-1"/><error statusCode="404" prefixLanguageFilePath="" path="/302page.asp" responseMode="ExecuteURL"/><error statusCode="500" prefixLanguageFilePath="" path="/500page.asp" responseMode="ExecuteURL"/><error statusCode="500" subStatusCode="100" path="/500page.asp" responseMode="ExecuteURL"/></httpErrors><system.webServer>

我通过 IIS 管理器在相关站点上进行了配置,但如果您更容易,您可以通过 web.config 文件进行配置.

您可以根据是否应为 301、302 或 404 添加条件标头.

404;

Response.Status = "404 Not Found"Response.AddHeader位置",页面名称

302(临时重定向);

Response.Status="301 Object Moved"Response.AddHeader位置",页面名称

301(永久重定向);

Response.Status="301 已永久移动"Response.AddHeader位置",页面名称

IIS 站点的应用程序池使用集成管道模式.并附上网站调试部分的设置.

We're migrating an existing website from IIS6 to IIS7, but are experiencing some difficulty in setting up the 404 error page. Our 404-errorpage works like this:

  • A custom ASP-page checks the URL against a short list of 'special' URLs (e.g. http://example.com/limited-offers).
  • If the URL is known, it redirects to the actual URL of that page.
  • Otherwise the visitor is redirected to a static errorpage with a 404-statuscode.

With IIS6 this worked as advertised, but with IIS7 some things have changed. IIS7 will always display the configured errorpage when it encounters a statuscode for which an errorpage is defined. In case of our static errorpage with 404-statuscode, this means that IIS7 will execute the custom ASP-page again. This leads to infinite redirection.

We've discovered that this behavior can be circumvented by adding a setting in Web.Config

<system.webServer>
  <httpErrors existingResponse="PassThrough" />
</system.webServer>

However, after adding this our custom ASP-page refuses to redirect. After checking with Fiddler it seems that IIS7 forces the 404 statuscode, overwriting our 302 redirect.

Can anyone recommend another approach to solve our problem?

解决方案

I successfully use a similar setup which I migrated from IIS 6 to IIS 7. My web.config has the following section;

<system.webServer>
    <httpErrors errorMode="Custom">
        <remove statusCode="500" subStatusCode="-1" />
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/302page.asp" responseMode="ExecuteURL" />
        <error statusCode="500" prefixLanguageFilePath="" path="/500page.asp" responseMode="ExecuteURL" />
        <error statusCode="500" subStatusCode="100" path="/500page.asp" responseMode="ExecuteURL" />
    </httpErrors>
<system.webServer>

I configured this on the relevant site via IIS Manager but you could do it via web.config file if easier for you.

You can add conditional header depending on whether should be 301, 302 or 404.

404;

Response.Status = "404 Not Found"
Response.AddHeader "Location", pagename

302 (temporary re-direct);

Response.Status="301 Object Moved"
Response.AddHeader "Location", pagename

301 (permanent re-direct);

Response.Status="301 Moved Permanently"
Response.AddHeader "Location", pagename

IIS site's application pool uses Integrated pipeline-mode. And attached are settings for debugging section for site.

这篇关于为 IIS7 网站配置带有重定向的自定义 ASP 404 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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