13

I am using the web.config code below to redirect requests to missing pages to a 404 error handling page:

<customErrors mode="On" defaultRedirect="404.aspx" redirectMode="ResponseRewrite">
  <error statusCode="404" redirect="404.aspx"/>
</customErrors>

It works fine when I look for pages such as "missing.aspx" but it does not work for pages without the ".aspx" extension such as "missing.asp" or just "missing". When it does not work, it just loads a standard IIS 7.5 error page.

What am I doing wrong? I am using .net 4. I noticed other people asking the same question but they didn't get an answer.

Thanks!

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Osprey
  • 1,523
  • 8
  • 27
  • 44
  • try removing `redirectMode="ResponseRewrite"` – Damith Jun 15 '12 at 06:42
  • I tried using the information in this question but was not able to get it going. Would you be so kind to take a look at http://stackoverflow.com/questions/25798775/web-config-not-forwarding-to-404-error-page-on-non-aspx-pages – JGallardo Sep 12 '14 at 00:47
  • This worked for me I have a website Project https://stackoverflow.com/a/48661494/6665876 – Ankit Kashyap Jun 03 '19 at 10:48

3 Answers3

10

As dbaseman states this is because the asp.net handlers are not called for non-asp.net files. An easy way to force the asp.net handler to operate on all requests is to set the following in your web.config.

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
</system.webServer>

This tells IIS to run through all of the managed modules for all requests such as .html, .jpg, .css, .js, etc. This is typically frowned upon as it just introduces extra processing and latency.

http://www.hanselman.com/blog/BackToBasicsDynamicImageGenerationASPNETControllersRoutingIHttpHandlersAndRunAllManagedModulesForAllRequests.aspx

Another option to try (cleaner than the above) was posted as an answer here: https://stackoverflow.com/a/6661699/701062

Community
  • 1
  • 1
Gary.S
  • 7,076
  • 1
  • 26
  • 36
  • I tried using what you wrote but could not make it work. would you mind taking a look at http://stackoverflow.com/questions/25798775/web-config-not-forwarding-to-404-error-page-on-non-aspx-pages – JGallardo Sep 12 '14 at 00:48
  • I love you... so much Gary S. – BossWalrus Sep 30 '14 at 19:47
4

The reason is that non-ASPX extensions never make it to the ASP.NET handler; those errors you see are coming from IIS. There is a separate section httpErrors under system.webServer in web.config that you will need to configure to handle these errors. See here for more info.

Example from the link:

<configuration>
   <system.webServer>
      <httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File" >
         <remove statusCode="500" />
         <error statusCode="500"
            prefixLanguageFilePath="C:\Contoso\Content\errors"
            path="500.htm" />
       </httpErrors>
   </system.webServer>
</configuration>
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • I added the code below but now instead of running the error handling page, it displays the source code of the page (when attempting to load an non existent non aspx page). ` ` – Osprey Jun 15 '12 at 14:08
  • 2
    This is the better answer as it does not rely on sending all request through the ASP.NET pipeline. @Osprey - this is because of the errorMode being set to "DetailedLocalOnly" This setting says, hey, if the request comes from the machine hosting the files, give some detailed info, else, show the 500 page I have defined. This is useful in debugging scenarios. You can change this setting to "Custom" and it will always display the friendly error message. – Tommy Jul 09 '14 at 12:06
  • This does not work. And why did you add for 505 when the question was specifically about the 404 error? – JGallardo Sep 11 '14 at 23:03
  • @JGallardo doesn't work locally, but should work on the server. I assume you can find/replace "404", or is that too much for you? – McGarnagle Sep 11 '14 at 23:08
  • @McGarnagle I obviously replaced with 404. And then I added it to the remote server but still did not work. – JGallardo Sep 11 '14 at 23:15
1

Add the following to the Web.config:

<system.webServer>    
    <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL" >
      <remove statusCode="404" />
      <error statusCode="404" path="/Default.aspx" responseMode="Redirect" />
    </httpErrors>
  </system.webServer>

I took Osprey's code, and added responseMode="Redirect" to fix the problem of just displaying the source code of the page.

Matt G
  • 71
  • 1
  • 1