Configuring custom ASP 404 page with redirect for IIS7 website

22,743

Solution 1

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.

settings for debugging section for site

Solution 2

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.

Solution 3

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.

Share:
22,743
Arjen
Author by

Arjen

Updated on November 12, 2020

Comments

  • Arjen
    Arjen over 3 years

    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?

  • Arjen
    Arjen about 13 years
    The 302page.asp redirects to another page? And if so, does it have a 404 HTTP statuscode?
  • Andy Davies
    Andy Davies about 13 years
    Yeah, sorry just edited that bit in. 302 or 404 conditional on what is being done within the code.
  • Arjen
    Arjen about 13 years
    That exactly what we're looking for, but can't get it to work. Do you have any special settings in IIS for the website or the application pool? For instance, does your application pool use the Classic or Integrated pipeline-mode?
  • Andy Davies
    Andy Davies about 13 years
    The application pools we use are Integrated pipeline-mode. The pools were created and configured automatically when creating site by IIS - we didn't change them manually.
  • Arjen
    Arjen about 13 years
    Our application pools are running in the Classic pipeline-mode. I'll check to see if changing it to Integrated will make a difference. Thanks for your feedback.