Custom Error Pages and 404 Page Not Found Error Web.Config Setup

When receiving 404 page not found errors on your web site, and you wish to direct users back to your home page, you need to setup two things in your web.config to handle all instances.

If you use IIS7 and go to the Error Pages section and setup the 404 error to "Execute a URL on this site" and have it go to : /default.aspx and select OK to save the changes, it will update your system.webServer section of your web.config file and add these lines:

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

This will redirect any pages such as http://www.shiningstar.net/test with no file extensions. But if the URL sends the user to a page with a known file extension, they will still be taken to the 404 error page.

So if you want to redirect a page such as http://www.shiningstar.net/test.aspx you will also need to add custom error handling to your system.web section of your web configuration file:

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


The 3 options for the mode are: Off, On, and RemoteOnly. To test your error handling locally, change the mode to "On." Once you've tested it, change it to "RemoteOnly" which will still allow you to receive descriptive errors when developing locally.

[SIGNATURE]

3 Comments

Comments have been disabled for this content.