How to configure a Web.Config file to allow custom 404 handling while still displaying on-page 500 error detail?

62,327

Solution 1

Try this -

<configuration>
   <system.webServer>
      <httpErrors errorMode="Custom" defaultResponseMode="File" >
         <remove statusCode="404" />
         <remove statusCode="500" />
         <error statusCode="404" 
            path="404.html" />
         <error statusCode="500" 
            path="500.html" />
       </httpErrors>
   </system.webServer>
</configuration>

The 404 error page is 404.html and it is located in my root, likewise the 500 page

Solution 2

Classic ASP doesn't pass through the error in IIS7 by default. To enable that for a site, run the following from the command line, being sure to replace {sitename} with your name site:

c:\windows\system32\inetsrv\appcmd.exe set config "{sitename}" -section:system.webServer/httpErrors /existingResponse:"PassThrough" /commit:apphost

Additionally, make sure to turn off IIS friendly errors. They really aren't friendly.

Solution 3

I am discovering a lot of complicated work-arounds for Classic ASP in IIS 7.5 for dealing with IIS 6 type of custom error handling for a specific error code, typically 404. I hope this helps people who stumble upon it.

I am running IIS 7.5 R2 but I think this applies to IIS 7 in general.

If your web.config file has this in it:

<customErrors mode="On">

...inside the IIS Manager you probably selected Edit Features under Error Pages to change the features on the Edit Error Pages Settings panel. If you change this to Custom Error Pages (customErrors mode="On"), you are forced to setup handling of each and every error number. So don't do that.

All you have to do is RIGHT click on the error number you wish to handle and select Edit. There you can point to a custom error page you create or redirect to a URL. Rather than create a custom error page that you have to remember to update when your site design or menus change, I always suggest redirecting to the home page and add something to the querystring like ?syserror which you can then handle from the home page. My technique is to check for this in the home page scripting and popup a friendly message letting them know their page was not found and they were directed to the home page.

Your web.config file would look something like this:

<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" 
path="http://yoursite.com/index.asp?syserror" responseMode="Redirect" />
</httpErrors>

I also suggest that you do not enable detailed error messages on a production Website as a security precaution, even for local. Just look up the details in your system log.

Just my 2 cents, hope it helps and please let me know if you think I am incorrect on anything here.

Solution 4

Just add another value for 500 errors.

<customErrors mode="On">
<error statusCode="404" redirect="/Custom404.html" />
<error statusCode="500" redirect="/Custom500.html" />
</customErrors>
Share:
62,327
squillman
Author by

squillman

Updated on September 17, 2022

Comments

  • squillman
    squillman over 1 year

    To customize 404 handling and based on the hosting company's suggestion, we are currently using the following web.config setup. However, we quickly realized that with this configuration, any page error (500 error) are also getting redirected to this custom error page. How can I modify this config file so we can continue to handle 404 with custom file while still able to view on-page error?

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.webServer>
    <httpErrors errorMode="DetailedLocalOnly" defaultPath="/Custom404.html" defaultResponseMode="ExecuteURL">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" prefixLanguageFilePath="" path="/Custom404.html" responseMode="ExecuteURL" />
    </httpErrors>
    </system.webServer>
    <system.web>
    <customErrors mode="On">
    <error statusCode="404" redirect="/Custom404.html" />
    </customErrors>
    </system.web>
    </configuration>
    
    • Admin
      Admin over 14 years
      Clarification, the script is actually classic asp and the server is II7. The goal is to redirect 404 results to a custom page while still being able to see error message if a page runs into 500-type of errors (instead of redirecting to a custom 500 error page). At current setup, 404 and 500 errors will always reach /custom404.html.
  • Admin
    Admin over 14 years
    Thank you for the answer, but this did not work for me. A page with 500 error still redirects to /custom404.html after including your suggestion. I've added some additional note below the original question, hopefully clarifies my goal a bit.
  • Pacerier
    Pacerier over 8 years
    Is the ordering important?
  • Stephen
    Stephen about 6 years
    I added it to web.config for the site. <httpErrors existingResponse="PassThrough"></httpErrors>