为IIS7网站配置重定向的自定义ASP 404页面

Arj*_*jen 2 error-handling iis-7 custom-error-pages asp-classic http-status-code-404

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

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

有了IIS6,这就像宣传的一样,但是在IIS7中,有些东西已经改变了.当IIS7遇到定义了errorpage的状态码时,它将始终显示已配置的错误.如果我们的静态错误与404状态代码,这意味着IIS7将再次执行自定义ASP页面.这导致无限重定向.

我们发现通过在Web.Config中添加设置可以避免这种行为

<system.webServer>
  <httpErrors existingResponse="PassThrough" />
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

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

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

And*_*ies 6

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

<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>
Run Code Online (Sandbox Code Playgroud)

我通过IIS管理器在相关站点上配​​置了这个,但是你可以通过web.config文件来实现它,如果你更方便的话.

您可以添加条件标头,具体取决于是301,302还是404.

404;

Response.Status = "404 Not Found"
Response.AddHeader "Location", pagename
Run Code Online (Sandbox Code Playgroud)

302(临时重新直接);

Response.Status="301 Object Moved"
Response.AddHeader "Location", pagename
Run Code Online (Sandbox Code Playgroud)

301(永久重新直接);

Response.Status="301 Moved Permanently"
Response.AddHeader "Location", pagename
Run Code Online (Sandbox Code Playgroud)

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

用于调试站点的部分的设置