Web.config defaultDocument
The web.config file can be used to set a default document, or list of default documents for your website. Web.config can be used to change the default document (page) for an entire site, or on a directory by directory basis. The default page may be a .php, .asp, .htm, .aspx, .cfm, or any other file type handled by the web server.
While some web.config sections require that the containing directory is set as an application, this isn't one of them. A simple web.config with a defaultDocument section may be placed in any directory, and the directory does NOT need to be set as an application.
The default document is returned to the client when the browser requests a directory name (foo.com/, foo.com/somedir/), but doesn't specify a page. If there isn't a default document in the directory, the client will receive a "file not found" or "directory browsing denied" error. The web server is configured to search for default files in the order listed below. If you wish to chage the default document order or add another file name, please follow the directions on this page.
- index.htm
- index.html
- default.asp
- default.aspx
- index.asp
- index.aspx
- index.cfm
- index.php
- default.htm
How it's done
Example default document list. Comments are enclosed in <!-- --> and are not required.
- Clear all default documents, then add a list of documents
<defaultDocument enabled="true"> <!-- this line enables default documents for a directory --> <files> <clear/> <!-- removes the existing default document list --> <add value="foo.htm"/> <!-- foo.htm is now the default document --> <add value="foo.php"/> <!-- 2nd default document in search order --> <add value="foo.aspx/> <!-- 3rd default document in search order --> <add value="foo.cfm/> <!-- 4th default document in search order --> </files> </defaultDocument>
Using Custom Default Documents
- Use a text editor to create a file named web.config
- Save the web.config file with the appropriate content
- Place the web.config file in the directory that you wish to modify
Detailed web.config content
- If there isn't an existing web.config in the directory, your new web.config should look something like this
<?xml version="1.0"?> <configuration> <system.webServer> <defaultDocument enabled="true"> <!-- this line enables default documents for a directory --> <files> <clear/> <!-- removes the existing default document list --> <add value="foo.htm"/> <!-- foo.htm is now the default document --> <add value="foo.php"/> <!-- 2nd default document in search order --> <add value="foo.aspx/> <!-- 3rd default document in search order --> <add value="foo.cfm/> <!-- 4th default document in search order --> </files> </defaultDocument> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
- If there is an existing web config, without a <system.webServer> section... Your new web.config should look like this
<?xml version="1.0"?> <configuration> <system.web> .. existing text .. .. existing text .. </system.web> <system.webServer> <defaultDocument enabled="true"> <!-- this line enables default documents for a directory --> <files> <clear/> <!-- removes the existing default document list --> <add value="foo.htm"/> <!-- foo.htm is now the default document --> <add value="foo.php"/> <!-- 2nd default document in search order --> <add value="foo.aspx/> <!-- 3rd default document in search order --> <add value="foo.cfm/> <!-- 4th default document in search order --> </files> </defaultDocument> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
- If your existing web.config already has a <system.webServer> section, just add the <defaultDocument> section
<?xml version="1.0"?> <configuration> <system.web> .. existing text .. .. existing text .. </system.web> <system.webServer> <defaultDocument enabled="true"> <!-- this line enables default documents for a directory --> <files> <clear/> <!-- removes the existing default document list --> <add value="foo.htm"/> <!-- foo.htm is now the default document --> <add value="foo.php"/> <!-- 2nd default document in search order --> <add value="foo.aspx/> <!-- 3rd default document in search order --> <add value="foo.cfm/> <!-- 4th default document in search order --> </files> </defaultDocument> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>