Share via


既定のドキュメント: <defaultDocument>

概要

<defaultDocument> 要素を使うと、Web サイトまたはアプリケーションでの既定のドキュメントの動作を有効または無効にできます。 この要素には <files> 子要素が含まれ、それには <add> 要素のコレクションが含まれています。 各 <add> 要素では、サイトがルート ディレクトリへの要求を受け取ったときに、サイトまたはアプリケーションがクライアント ブラウザーに返す既定のドキュメントが定義されています。

<defaultDocument> 要素には、1 つ以上の <remove> 要素を含めることもできます。 各 <remove> 要素は、インターネット インフォメーション サービス (IIS) 7 サーバー上の上位の構成ファイルから継承された特定の既定のドキュメント ファイルを削除します。 <clear> 要素を使って、構成階層の上位で定義されているすべての既定のドキュメントの設定を削除することもできます。

既定のドキュメントは既定で有効になり、IIS 7 では、サーバー全体の既定値として、ApplicationHost.config ファイルで次の既定のドキュメント ファイルが定義されています。

  • Default.htm
  • Default.asp
  • Index.htm
  • Index.html
  • Iisstart.htm

(Web サーバーに ASP.NET をインストールすると、インストール プロセスによって Default.aspx ファイルがこの一覧に追加されます)。

既定では、サイトまたはアプリケーションのルート ディレクトリに対する要求が届くと、IIS 7 は、ユーザーが <defaultDocument> 要素で定義したファイル名に基づいてブラウザーに応答を送信します。 IIS は、<defaultDocument> 要素に出現する順序で正しいファイルを返そうとします。 Default.htm ファイルが存在する場合、IIS はそれをクライアント ブラウザーに送信します。 Default.htm ファイルが存在しない場合、IIS は Default.asp ファイルをブラウザーに送信しようとします。Default.asp ファイルが存在しない場合、IIS は Index.htm ファイルをブラウザーに送信しようとします。 IIS は、この方法で、各既定のドキュメント ファイルのクライアント ブラウザーへの送信の試みを続けます。

Note

ユーザーがこの既定のドキュメントの一覧を正しく並べた場合、IIS のパフォーマンスを向上させることができます。 たとえば、Web サイトのすべてのフォルダーでホーム ページに Index.php を使っていて、Index.php を既定のドキュメントの一覧の最後に追加した場合、IIS は、クライアント ブラウザーに Index.php の返送を試みる前に、一覧内の既定のドキュメントを 1 つずつ調べる必要があります。 Web サイト内のフォルダーの数と、クライアント ブラウザー要求の数によっては、パフォーマンスに影響する可能性があります。

既定のドキュメントを無効にしていても、ディレクトリの参照を有効にしてある場合、ブラウザーからルート ディレクトリの要求が届くと、IIS はディレクトリの一覧を返します。 既定のドキュメントとディレクトリの参照の両方を無効にした場合、IIS は "HTTP 404 ファイルが存在しません" というエラーをブラウザーに送信します。

互換性

バージョン メモ
IIS 10.0 <defaultDocument> 要素は、IIS 10.0 では変更されませんでした。
IIS 8.5 <defaultDocument> 要素は、IIS 8.5 では変更されませんでした。
IIS 8.0 <defaultDocument> 要素は、IIS 8.0 では変更されませんでした。
IIS 7.5 <defaultDocument> 要素は、IIS 7.5 では変更されませんでした。
IIS 7.0 <defaultDocument> 要素は IIS 7.0 で導入されました。
IIS 6.0 <defaultDocument> 要素は、IIS 6.0 の DefaultDoc プロパティと、IIsWebService メタベース オブジェクトの DirBrowseFlags プロパティの EnableDefaultDoc 値を置き換えます。

段取り

<defaultDocument> 要素は IIS 7 の既定のインストールに含まれています。

操作方法

アプリケーションまたはサイトの既定のドキュメントを追加する方法

  1. インターネット インフォメーション サービス (IIS) マネージャーを開きます。

    • Windows Server 2012 または Windows Server 2012 R2 を使用している場合:

      • タスク バーで、[サーバー マネージャー] をクリックし、[ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順にクリックします。
    • Windows 8 または Windows 8.1 を使用している場合:

      • Windows キーを押しながら文字 X を押し、[コントロール パネル] をクリックします。
      • [管理ツール] をクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
    • Windows Server 2008 または Windows Server 2008 R2 を使用している場合:

      • タスク バーの [スタート] をクリックし、[管理ツール] をポイントして、[インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。
    • Windows Vista または Windows 7 を使用している場合:

      • タスク バーで、[スタート][コントロール パネル] の順にクリックします。
      • [管理ツール] をダブルクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
  2. [接続] ウィンドウで、サーバー名を展開し、[サイト] を展開して、既定のドキュメントを構成する Web サイトまたはアプリケーションに移動します。

  3. [ホーム] ウィンドウで、[既定のドキュメント] をダブルクリックします。
    Screenshot of the Home pane with the Default Document option being highlighted.

  4. [操作] ウィンドウで、[追加] をクリックします。

  5. [既定のドキュメントの追加] ダイアログ ボックスで、追加する既定のドキュメントの名前を [名前] ボックスに入力して、[OK] をクリックします。
    Screenshot of the Add Default Document dialog box.

  6. 必要な場合は、[操作] ウィンドウの一覧で既定のドキュメントを選び、[上へ移動] または [下へ移動] をクリックして、IIS が既定のドキュメントの一覧を検索する順序を定義します。

  7. [既定のドキュメント] 警告ボックスで、[はい] をクリックして親構成レベルからの構成の継承を拒否するか、[なし] または [キャンセル] をクリックして既定のドキュメントの順序の変更を取り消します。
    Screenshot of the Default Document alert box with a focus on the Yes option.

  8. 必要な場合は、[操作] ウィンドウの [削除] をクリックして、既定のドキュメントとして使わないファイル名を削除します。

構成

<defaultDocument> 要素内の <add> 要素の順序を変更して、IIS が既定のドキュメントを処理する順序を変更できます。

<defaultDocument> 要素は、ApplicationHost.config ファイルを使ってサーバー レベルで、および適切な Web.config ファイルを使ってサイト、アプリケーション、または URL レベルで構成できます。

属性

属性 説明
enabled 省略可能な Boolean 属性です。

既定のドキュメントを有効にするかどうかを指定します。

既定値は true です。

子要素

要素 説明
files 省略可能な要素です。

既定のドキュメントとして返すことができるファイル名の一覧を指定します。

構成サンプル

次の構成例をサイトまたはアプリケーションの Web.config ファイルに含めると、サイトまたはアプリケーションの既定のドキュメントが有効になります。 次に、サイトまたはアプリケーションの既定のドキュメントの一覧にファイル名 "Home.html" を追加します。

<configuration>
   <system.webServer>
      <defaultDocument enabled="true">
         <files>
            <add value="home.html" />
         </files>
      </defaultDocument>
   </system.webServer>
</configuration>

サンプル コード

次の例では、Contoso という名前の Web サイトで既定のドキュメントを有効にした後、Home.html という名前のファイルをサイトの既定のドキュメントの一覧に追加しています。

AppCmd.exe

appcmd.exe set config "Contoso" /section:defaultDocument /enabled:true /+files.[value='home.html']

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample {

    private static void Main() {
        
        using(ServerManager serverManager = new ServerManager()) { 
            Configuration config = serverManager.GetWebConfiguration("Contoso");
            
            ConfigurationSection defaultDocumentSection = config.GetSection("system.webServer/defaultDocument");
            
            defaultDocumentSection["enabled"] = true;
            
            ConfigurationElementCollection filesCollection = defaultDocumentSection.GetCollection("files");
            ConfigurationElement addElement = filesCollection.CreateElement("add");
            addElement["value"] = @"home.html";
            filesCollection.AddAt(0, addElement);
            
            serverManager.CommitChanges();
        }
    }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Class Sample
   Shared Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetWebConfiguration("Contoso")
      Dim defaultDocumentSection As ConfigurationSection = config.GetSection("system.webServer/defaultDocument")

      defaultDocumentSection("enabled") = True

      Dim filesCollection As ConfigurationElementCollection = defaultDocumentSection.GetCollection("files")
      Dim addElement As ConfigurationElement = filesCollection.CreateElement("add")
      addElement("value") = "home.html"
      filesCollection.AddAt(0, addElement)

      serverManager.CommitChanges()
   End Sub
End Class

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso";

var defaultDocumentSection = adminManager.GetAdminSection("system.webServer/defaultDocument",
   "MACHINE/WEBROOT/APPHOST/Contoso");

defaultDocumentSection.Properties.Item("enabled").Value = true;

var filesCollection = defaultDocumentSection.ChildElements.Item("files").Collection;

var addElement = filesCollection.CreateNewElement("add");
addElement.Properties.Item("value").Value = "home.html";
filesCollection.AddElement(addElement, 0);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso"

Set defaultDocumentSection = adminManager.GetAdminSection("system.webServer/defaultDocument", _
   "MACHINE/WEBROOT/APPHOST/Contoso")

defaultDocumentSection.Properties.Item("enabled").Value = True  

Set filesCollection = defaultDocumentSection.ChildElements.Item("files").Collection

Set addElement = filesCollection.CreateNewElement("add")
addElement.Properties.Item("value").Value = "home.html"
filesCollection.AddElement addElement, 0

adminManager.CommitChanges