Configuring custom ASP 404 page with redirect for IIS7 website

折月煮酒 提交于 2019-11-28 02:10:26

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.

I encountered a similar scenario for a client the other week. The solution was to configure your <httpErrors> as follows:

<httpErrors errorMode="Custom" existingResponse="Auto">

  <remove statusCode="404" subStatusCode="-1" />
  <error statusCode="404" 
         prefixLanguageFilePath="" 
         path="/404.asp" 
         responseMode="ExecuteURL" />

  <remove statusCode="500" subStatusCode="100" />
  <error statusCode="500" 
         subStatusCode="100" 
         prefixLanguageFilePath="" 
         path="/500-100.asp" 
         responseMode="ExecuteURL" />

</httpErrors>

This works with Cactushop (written in Classic ASP) which has "friendly" urls and where they use the 404 handler page to parse the url and render products or list categories of products and so on.

Alternatively you could look at using the IIS addon module: URLRewrite. This will allow you to set up custom SEO friendly URL's. You might find this something you want to look at to improve your application going forward rather than as a fix for your existing issue as may require some time to learn.

There's some excellent articles, video tutorials and info on how to use this tool.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!