asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search










Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Custom Error Pages with IIS 4.0
By Wayne Berry
Rating: 3.9 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    This Issue

    In this issue, we will discuss how to use custom error pages with IIS 4.0. With IIS 4.0, you can return special pages for each web site error instead of the default pages. So instead of the message "404 File Not Found," the user could have a list of optional links with your company header and an apologetic message. We will demonstrate how to get the most from custom error pages by using Active Serve pages to customize the message.

    Error Messages

    For every standard HTTP error 403, 404, 504, a message is returned to the browser. IIS 4.0 allows you to return the default error message, return a specific web page, or return a file as a web page for each error. Along with the standardized breakdown, IIS 4.0 subdivides the individual errors into specific problems within the server. For each specific problem you can return a different message.

    When installed, IIS 4.0 installs error files at:

    
    c:\winnt\help\common
    
    
    The custom errors are mapped to these files. Table 1 shows the error message returned from the installed files.

    Table 1 : Customizable Error Messages
    StatusMessage
    400Bad Request
    401.1Unauthorized: Logon Failed
    401.2Unauthorized: Logon Failed due to server configuration
    401.3Unauthorized: Unauthorized due to ACL on resource
    401.4Unauthorized: Authorization failed by filter
    401.4Unauthorized: Authorization failed by filter
    401.5 Unauthorized: Authorization failed by ISAPI/CGI app
    403.1 Forbidden: Execute Access Forbidden
    403.1 Forbidden: Execute Access Forbidden
    403.2 Forbidden: Read Access Forbidden
    403.3Forbidden: Write Access Forbidden
    403.4Forbidden: SSL required
    403.5Forbidden: SSL 128 required
    403.6Forbidden: IP address rejected
    403.7Forbidden: Client certificate required
    403.8Forbidden: Site access denied
    403.9Access Forbidden: Too many users are connected
    403.10Access Forbidden: Invalid Configuration
    403.11Access Forbidden: Password Change
    403.12Access Forbidden: Mapper Denied Access
    404File Not Found
    405Method Not Allowed
    406Not Acceptable
    407Proxy Authentication Required
    412Precondition Failed
    414Request-URI Too Long
    500Internal Server Error
    501Not Implemented
    502Bad Gateway

    These files are typical of any web server's response. Just enough information to solve the problem but outside of the character of the web site. Most professional sites modify their error messages, and now with IIS 4.0 you can also.

    Simple Modifications

    In a typical situation you will want to just to add your company’s header to the error page and offer a link back to the home page. To do this you can modify the installed files in:

    
    c:\winnt\help\common
    
    
    By modifying these files, you change all the errors for the whole server. Treat the file as you would any HTML page, you can add HTML and client-side script. However, the page doesn't get executed like an Active Server page would so do not add server-side scripting.

    Multiple Web Servers

    If there are multiple web sites on the same machine, you can not modify the default files with the site header, since the default files will be used for all the sites. In order to create custom error messages you will need to take a copy of the files on a per web site basis. Try making a virtual root out of the root of the domain, called errors, and make a copy of the default files to that directory. With the files within the scope of the web site it is easier to remember to back up the error files. Secondly, you can change IIS 4.0 custom mapping from a file to an URL. Here is how:

    1. Open up MMC.
    2. Choose the web site, where you want to map custom error messages, by clicking on the site within the MMC tree.
    3. Right click and choose Properties from the drop down menu.
    4. From with the properties dialog choose the Custom Error tab.

      Figure 1 : Custom Headers


    5. From the Error messages for HTTP errors list, choose the error you wish to map.
    6. Once the error is chosen, click on Edit Properties button near the bottom of the dialog.
    7. Within the Error mapping Properties dialog change the Message type to URL.
    8. In the URL text box type in the URL of the new custom error.
      Note
      The URL must be within the scope of the web site and reference the page off of the virtual root. Here is an example of an URL that will work:
      
      \errors\404.htm
      
      
      These examples will not work:
      
      http://www.myserver.com/errors/404.htm
      404.htm
      http://www.someotherserver.com/errors/404.htm
      ../errors/404.htm
    9. Click on OK, then OK again to save the changes.
    This is probably the simplest way to modify the error message on a per-server basis, however there are things that can be done to make the user's life easier.

    Sending the User Home

    One of the most common error messages is the "404 File Not Found." Sites that change often are always running into the problem with bad bookmarks, or links from other sites pointing to a missing page. Users that receive 404 error message usually never make it to the correct page, or even a useful page on your web site. To avoid this you can map the 404 error message user to the home page of your site. This is an easy solution for the user. To do this, determine the home page, such as default.htm, and use the instructions noted above to map the 404 error message to the home page.

    Dynamic Error Messages

    With IIS 4.0, it is possible to return an Active Server page instead of a static .htm page. All you have to do is create an Active Server page within the scope of the web site, and use the technique above to map the page to an Active Server page. The server will execute the page and return the response to the user.

    Note
    Mapping error messages to Active Server pages is probably only a good idea when with messages are in the 400-499 range. Messages in the 500-599 range indicate a server problem and the Active Server page might not be able to execute when the original message is in the 500-599 range.
    An interesting trick is to take a copy of the default page, change the extension to .asp and map the correct errors to the correct pages. Once you have done that you can use #include to include your company’s header in all the pages, instead of modifying all the pages individually. Just create a file named header.inc like Example 1.

    Example 1 : header.inc

    
    <H1>My Company</H1>
    <H3>blah blah blah blah</H3>
    
    
    Then in each error page include the header file like in Example 2.

    Example 2 : Including the Header

    
    <!--#include virtual="/errors/header.inc"-->
    
    
    This could possibly be the same header that you use in all the web pages on your site, the working pages and the error pages. Mapping the error to an Active Server page opens the door for a multitude of opinions.

    Getting Creative

    When there is a mapping from an error message to an Active Server page, IIS 4.0 sends along additional information in the URL encoded string. In other words, if you have the 404 error mapped to 404.asp, when the 404.asp page is called, the server sends the error status, 404, and the request from the browser that caused the error.

    Actually, the server just redirects the browser to the page, instead of calling the page and returning. This is important to understand, here are the steps in order:

    1. User enters an address into the browser.
    2. The browser makes a request to the server.
    3. The server determines that page doesn't exist.
    4. The server consults the custom error map and looks up the new URL.
    5. Instead of returning a 404 status to the browser the server returns a 301, with a URL.
    6. The browser determines the URL is a redirection and calls the URL that was returned from the server.
    7. The URL returned from the server is displayed in the browser.

    You can extract the bad request URL from within the Active Server page that is handling the error by parsing the URL encoded string. Example 3 shows how to create a 404.asp page that will return a more interactive page to the user.

    Example 3 : 404.asp

    
    <HTML>
    <BODY>
    <%
    	strQString=Request.ServerVariables("QUERY_STRING")
    	If (Len(strQString)>4) Then
    		strURL=Right(strQString,Len(strQString)-4)
    	End If
    %>
    <H1><%=strURL%>, can not be found</H1>
    </BODY>
    </HTML>
    
    

    Parsing the URL-encoded string is a very powerful tool, now that we understand how to handle errors we can create errors that we want to handle.

    Requesting Pages that Do Not Exist

    What if you were to use the custom error message feature of IIS 4.0 to redirect a page request for a page that you knew did not exist? Imagine this scenario, you have a database full of knowledge based articles. You can access individual articles like article 100 with the following URL.

    
    http://www.myserver.com/kb/article.asp?ArctileId=100
    
    
    However you want to make the scenario simpler on your users. You want them to be able to enter:
    
    http://www.myserver.com/kb/100
    
    
    To solve this problem you make sure that file 100 doesn't exist. Then you map all 404 errors to 404.asp. Finally modify 404.asp to look like example 4.

    Example 4 : Modified 404.asp

    
    <%
    	Response.Expires=0
    	strQString=Request.ServerVariables("QUERY_STRING")
    	If (Len(strQString)>4) Then
    		Id=Right(strQString,Len(strQString)-4)
    		Response.Redirect("/kb/Article.asp?ArticleId=" & Id)
    	End If
    %>
    
    
    Note
    This example will cause an endless loop if article.asp does not exist. The server will serve a 404 error for article.asp that will redirect to 404.asp which will redirect to article.asp again.

    Bugs

    There is a bug in the custom error page that you should be aware of. If you are within a different directory then the 404 error redirects to the browser will become lost. For example, you are in /sale/page.htm and you link to a bad link taking you to /default.htm because of a custom error, the browser will think that you are in /sale/default.htm. You can avoid this problem by redirecting to /404.asp with the contents of 404.asp being this:

    Example 5 : Bug Free 404.asp

    
    <%
    Response.Redirect("/default.htm")
    %>
    
    
    This double redirect will avoid the problem.

    Secondly, crawlers, automated programs that crawl your web site, may become lost if you redirect to the home page when there is a 404 error.

    Summary

    The Internet Information Server allows the administrator to return files, or redirect to another page when an error occurs on a request to the server. Because the server allows you to redirect to another page, you can redirect to an Active Server page and when directing to an Active Server page the server sends additional information allow for a dynamic response.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Sep 29, 2005 - Migrating to a Load Balanced IIS 6 Environment
    Migration to IIS 6 can present itself as a daunting challenge. Depending on your existing hosting configuration, the process can number in hours, days, or even weeks. Careful planning and research is integral to achieve a successful migration.
    [Read This Article]  [Top]
    Apr 18, 2003 - IIS 6.0: Lessons in Trustworthy Computing
    Microsoft's Trustworthy Computing initiative significantly changed the way in which Microsoft builds and designs software. In this article, Jeff Gonzalez explores some of the new options and architecture in Internet Information Services 6.0.
    [Read This Article]  [Top]
    Mar 25, 2002 - Moving IIS To A Different Server - Part 2
    Brien Posey evaluates two additional methods for migrating a Web server and it settings, backup/restore and ghosting.
    [Read This Article]  [Top]
    Feb 27, 2002 - Moving Your IIS Server to a New Server - Part 1
    Upgrading your server? Brien Posey takes a look at the process and pitfalls of migrating IIS to a completely different server.
    [Read This Article]  [Top]
    Jan 23, 2002 - Troubleshooting IIS Access Problems
    Spending countless hours developing a Web site only to discover that no one can access it is frustrating. This article guides you through the process of troubleshooting Web-site access problems.
    [Read This Article]  [Top]
    Jan 18, 2002 - Running IIS on Windows XP Home Edition?
    Members of the 15Seconds discussion list may have found a way to run IIS on Windows XP Home Edition, so developers can run ASP pages. Attempt at your own risk!
    [Read This Article]  [Top]
    Dec 27, 2001 - Working With IIS Packet Filtering
    Brien Posey discusses IP packet filtering and other ways in which to control access through IIS.
    [Read This Article]  [Top]
    Oct 30, 2001 - Protecting Your IIS Server and Web Application
    Internet viruses such as Code Red and Nimbda have brought down numerous IIS Web servers recently. Fortify and defend your system with this comprehensive strategy authored by 30-year industry veteran, Andrew Novick.
    [Read This Article]  [Top]
    Oct 16, 2001 - Implementing an E-mail Content Filter Using CDO
    Stop SPAM from sliding through your e-mail system. George Walker shows how to create an e-mail content filter for the Windows 2000 SMTP service using Microsoft Collaboration Data Objects.
    [Read This Article]  [Top]
    Feb 10, 2000 - Creating Dynamic JavaScript with ASP and Databases
    Travis Giggy demonstrates how to put ASP tags inside of JavaScript blocks so developers can fit large amounts of data into one form on a single page. He offers an overview of things that can be done with dynamic JavaScript with ASP and data queries.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers