Custom Error for non ASP.NET files

Custom errors are helpful in website to provide information to the users with more friendly errors. I needed to do the same with my blog. But unfortunately only .aspx, .axd and other files would show the custom error. So navigating to a url ( that doesn’t exist and is not registered by would lead me to the following error which was not what I wanted.

I needed to extend it in order to support extension less url or other url that are not handled by

By default BlogEngine provides me with the following custom error which only works for files.

<customErrors mode="RemoteOnly" defaultRedirect="~/error404.aspx">
  <error statusCode="404" redirect="~/error404.aspx"/>

The solution was to modify the web.config, rather add the following piece of code to my <system.webServer> so that it gets handled by IIS (This requires IIS7).

<httpErrors errorMode="Custom" existingResponse="Replace" >
  <remove statusCode="404"/>
  <error statusCode="404" path="/error404.aspx" responseMode="ExecuteURL"/>

Now even non files are handled like shown below.

Note: In path="/error404.aspx", it start with / not ~/. ASP.NET users should be careful with this otherwise it will not work.

For more information on <httpErrors> refer to the MSDN Library.