|
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
| Status | Message |
| 400 | Bad Request |
| 401.1 | Unauthorized: Logon Failed |
| 401.2 | Unauthorized: Logon Failed due to server configuration |
| 401.3 | Unauthorized: Unauthorized due to ACL on resource |
| 401.4 | Unauthorized: Authorization failed by filter |
| 401.4 | Unauthorized: 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.3 | Forbidden: Write Access Forbidden |
| 403.4 | Forbidden: SSL required |
| 403.5 | Forbidden: SSL 128 required |
| 403.6 | Forbidden: IP address rejected |
| 403.7 | Forbidden: Client certificate required |
| 403.8 | Forbidden: Site access denied |
| 403.9 | Access Forbidden: Too many users are connected |
| 403.10 | Access Forbidden: Invalid Configuration |
| 403.11 | Access Forbidden: Password Change |
| 403.12 | Access Forbidden: Mapper Denied Access |
| 404 | File Not Found |
| 405 | Method Not Allowed |
| 406 | Not Acceptable |
| 407 | Proxy Authentication Required |
| 412 | Precondition Failed |
| 414 | Request-URI Too Long |
| 500 | Internal Server Error |
| 501 | Not Implemented |
| 502 | Bad 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:
- Open up MMC.
- Choose the web site, where you want to map custom error messages, by clicking on the site within the MMC tree.
- Right click and choose Properties from the drop down menu.
- From with the properties dialog choose the Custom Error tab.
Figure 1 : Custom Headers
- From the Error messages for HTTP errors list, choose the error you wish to map.
- Once the error is chosen, click on Edit Properties button near the bottom of the dialog.
- Within the Error mapping Properties dialog change the Message type to URL.
- 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
- 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:
- User enters an address into the browser.
- The browser makes a request to the server.
- The server determines that page doesn't exist.
- The server consults the custom error map and looks up the new URL.
- Instead of returning a 404 status to the browser the server returns a 301, with a URL.
- The browser determines the URL is a redirection and calls the URL that was returned from the server.
- 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.
|