Mixed default-document names can cause headaches when moving a site between Unix and Windows web servers. With one default name configured, a server looking for default.htm can miss a directory's index.html. NCSA and Apache let administrators specify an ordered list. Microsoft also announced multiple-default-document support in IIS 3.0 on December 10, 1996. My script addresses the file-selection problem in ASP.
This matters because Unix web sites use index.html by convention, and IIS sites use default.htm. When you migrate a site from Unix to IIS, or when you have content that was authored for both platforms, you end up with directories that have index.html files but no default.htm. A user browsing to that directory gets an error or a directory listing instead of the page they should see.
I wrote a solution: an ASP script called default-index.asp. You set this as your default document in IIS and put a copy in each directory that needs it. When a user requests a directory, the script tries these files in order:
- index.html
- index.stm
- index.shtml
- index.htm
- default.htm
- index.txt
- index.asp
The script selects the first file it can open. If it cannot open any of them, it writes "No default document found." For a site with mixed naming conventions, the ordered search avoids renaming all the existing default pages.
default-index.asp → index.html → index.stm → index.shtml → index.htm → default.htm → index.txt → index.asp
IIS calls default-index.asp as the default document. The script uses FileSystemObject.OpenTextFile to try each candidate in the current directory. For index.html, index.htm or default.htm, it reads the file and writes its contents to the response. For the other selected names, it uses Response.Redirect, so the browser requests that file and the address changes.
I put this on my web site when I wrote it, and it turned out to be the only solution available for this problem for quite a while. It was listed on the ASP Developers FAQ as the way to handle multiple default documents on IIS. I have gotten email from web administrators at various organizations saying they use it.
Later reflection: I had started writing an ISAPI filter version, intended to avoid interpreting ASP on each request. My software page later noted that the IIS 4.0 beta had multiple-default-document handling built in. If the server does the job, my script becomes unnecessary, which is fine. The best outcome for a workaround is that the vendor fixes the underlying problem.
The ASP source is available on my web site.
Here is the full source code. Its text-copying loop calls ReadLine and then SkipLine, which drops the next line; an adapted copy should remove that extra skip and preserve the line breaks. The original listing follows unchanged.
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
' File Name: default.asp
' Language: VBScript (Server Side)
' Purpose: Allows multiple default documents in Microsoft IIS WWW Service.
' Requires: Microsoft IIS 3.0 with Active Server Pages.
' Author: Rajiv Pant (Betul) [email protected] https://rajiv.org
' A better soultion than this would be use an ISAPI filter DLL to enable
' multiple default documents. I will post an example of that here.
' How to use: Set your IIS default document to the name of this file.
' (You can give any name to this file.) Then have a script
' place a copy of this file in every folder under your document
' root. (That is why the ISAPI version I'm writing is a better solution.)
' Note: You may want to modify this program to go through a list
' of default documents taking different kinds of actions for
' that type of default document.
' This system is useful for NT/IIS based ISPs that host many
' people's sites, some of which may be migrating from Unix
' based web servers. This way, people can continue using the
' default documents they were used to.
On Error Resume Next
doc = "index.html" ' We first look for index.html
set FileObject = Server.CreateObject ("Scripting.FileSystemObject")
Selected = FALSE
while NOT Selected
strFilename = Server.MapPath (doc) ' The physical path to the file doc
Err.clear
set oInStream = FileObject.OpenTextFile (strFilename, 1, FALSE, FALSE)
If (Err.number > 0) then
Select Case doc
' If index.html is not found, we look for index.stm
Case "index.html" doc = "index.stm"
' If index.stm is not found, we look for index.shtml
Case "index.stm" doc = "index.shtml"
' If index.shtml is not found, we look for index.htm
Case "index.shtml" doc = "index.htm"
' If index.htm is not found, we look for default.htm
Case "index.htm" doc = "default.htm"
' If default.htm is not found, we look for index.txt
Case "default.htm" doc = "index.txt"
' If index.txt is not found, we look for index.asp
Case "index.txt" doc = "index.asp"
' If even the last default document in our list is not found,
' we set doc to empty and use that to take certain action later.
Case Else doc = ""
Selected = TRUE
End Select
Else Selected = TRUE
End If
Wend
If doc = "" Then
' Now we could either display the directory like many web servers do,
' or we could just give a message as we do below.
Response.Write ("No default document found.<br>")
Else
If doc = "index.html" or doc = "index.htm" or doc = "default.htm" Then
' For .html, .htm, and other non-server-parsed files,
' open the file and print out it's contents. This way, the URL will
' not change and the browser will not have to do a redirection.
While NOT oInstream.AtEndOfStream
Response.Write (oInstream.Readline)
oInstream.Skipline()
Wend
Set oInstream = nothing
Else ' server-parsed or some other type of document
' For .stm, .shtml, .asp, .htx and other server-parsed or other files, redirect
' the browser to that file.
Response.Redirect (doc)
End If ' server-parsed or non-server-parsed document
End If
</SCRIPT>