HTTP リダイレクト <httpRedirect>

  • 概要
  • 互換性
  • セットアップ
  • 方法
  • 構成
  • サンプル コード

※本ページに挿入されている画像をクリックすると、画像全体が別ウィンドウで表示されます。

概要

<httpRedirect> 要素は、クライアント要求を新しい場所にリダイレクトするインターネット インフォメーション サービス (IIS) 7.0 の設定を構成します。

クライアントを新しい場所にリダイレクトしたいケースには、いくつかの理由が考えられます。たとえば、会社で新しい Web サイトに移行した場合は、古い Web サイトから新しい Web サイトにすべての要求をリダイレクトできます。同様に、Web サーバーに新しいアプリケーションを展開した場合、古いアプリケーションの URL 名前空間 (http://www.consoto.com/app\_v1.0/ など) のすべての要求を新しいアプリケーションの場所 (http://www.consoto.com/app\_v2.0/ など) にリダイレクトできます。

最も簡単な構成では、<httpRedirect> 要素の enabled 属性と destination 属性を設定するだけで、クライアントを新しい場所にリダイレクトできます。ただし、exactDestination 属性や httpResponseStatus 属性といった追加の要素を使用すると、入力したとおりのリダイレクト先の URL を返すかどうかを指定したり、Web クライアントに返す HTTP 応答コードを指定したりすることで、リダイレクトのエンドユーザー エクスペリエンスを構成できます。

互換性

  IIS 7.0 IIS 6.0
注意 <httpRedirect> は IIS 7.0 で新たに導入された要素です。 <httpRedirect> 要素は、IIS 6.0 の HttpRedirect メタベース プロパティに代わるものです。

セットアップ

HTTP リダイレクトは、IIS 7.0 の既定のインストールには含まれていません。インストールするには、次の手順を実行します。

Windows Server 2008

  1. タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [サーバー マネージャー] をクリックします。

  2. [サーバー マネージャー] ウィンドウのツリー表示で、[役割] を展開して [Web サーバー (IIS)] をクリックします。

  3. [Web サーバー (IIS)] ウィンドウで、[役割サービス] セクションまでスクロールして [役割サービスの追加] をクリックします。

  4. 役割サービスの追加ウィザードの [役割サービスの選択] ページで、[HTTP 共通機能] を展開し、[HTTP リダイレクト] を選択して、[次へ] をクリックします。

    拡大

  5. [インストール オプションの確認] ページで [インストール] をクリックします。

  6. [結果] ページで [閉じる] をクリックします。

Windows Vista

  1. タスク バーで [スタート] ボタンをクリックし、[コントロール パネル] をクリックします。

  2. コントロール パネルで、[プログラムと機能][Windows の機能の有効化または無効化] の順にクリックします。

  3. [Internet Information Services][World Wide Web サービス][HTTP 共通機能] の順に展開します。

  4. [HTTP リダイレクト] を選択して、[OK] をクリックします。

    拡大

方法

IIS 7.0 で HTTP ワイルドカード リダイレクトを追加するためのユーザー インターフェイスはありません。プログラムによって <httpRedirect> 要素に <add> 要素を追加する方法の例については、このドキュメントの「サンプル コード」セクションを参照してください。

HTTP リダイレクト規則を Web サイトまたはアプリケーションに追加する方法

  1. タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。

  2. [接続] ウィンドウで、当該サーバー名を展開して [サイト] を展開し、カスタム エラー ページを構成する Web サイトまたは Web アプリケーションを選択します。

  3. [ホーム] ウィンドウで [HTTP リダイレクト] をダブルクリックします。

    拡大

  4. [HTTP リダイレクト] ウィンドウで、要求をリダイレクトするチェック ボックスをオンにして、リダイレクト先の URL を入力します。

    拡大

  5. オプションで、次のいずれかを指定できます。

    • リダイレクト先を入力したとおりに構成します。
    • リダイレクト先を URL のサブフォルダーではなくルート フォルダーに限定したリダイレクト先に構成します。
    • HTTP 状態コードを次の 3 つのオプションのいずれかに構成できます。
      • 301 永続的
      • 302 検出
      • 307 一時的 : IIS 7.0 では、上記の各オプションに対して、それぞれ次の実際の HTTP 応答状態を返します。
      • HTTP/1.1 301 Moved Permanently
      • HTTP/1.1 302 Redirect
      • HTTP/1.1 307 Redirect
  6. これらの変更をすべて完了したら、[操作] ウィンドウで [適用] をクリックします。

構成

属性

属性 説明
childOnly オプションの Boolean 属性。

リダイレクトされる要求を含むファイル名の最初に、リダイレクト先の値を追加する必要があるかどうかを指定します。たとえば、childOnly が true に設定され、リダイレクト先の値が http://marking.contoso.com/ に構成された場合、https://contoso.com/default.htm に対する要求は http://marketing.contoso.com/default.htm にリダイレクトされます。

既定値は false です。
destination オプションの string 属性。

クライアントのリダイレクト先の URL または仮想パスを指定します。
enabled オプションの Boolean 属性。

リダイレクトを有効にする (true) か無効にする (false) かを指定します。

既定値は false です。
exactDestination オプションの Boolean 属性。

リダイレクト先の値に、相対的な場所ではなく、絶対的なターゲットの場所を設定することを指定します。

既定値は false です。
httpResponseStatus オプションの enum 属性。

リダイレクトの種類を指定します。

httpResponseStatus 属性には、次のいずれかの値を指定できます。既定値は Found です。

---------------------------------------------------------------------------------------------------------

値: Found

説明:

  • 302 状態コードを返します。これにより、destination 属性で指定した場所に新しい要求を発行するようにクライアントに指示します。
  • 数値は 302 です。

値: Permanent

説明:

  • 301 状態コードを返します。これにより、要求されたリソースの場所が永続的に変更されたことをクライアントに通知します。
  • 数値は 301 です。

値: Temporary

説明:

  • 307 状態コードを返します。これにより、ブラウザーで HTTP POST 要求が発行された場合、クライアントでデータが失われることを回避します。
  • 数値は 307 です。

子要素

要素 説明
add オプションの要素。

リダイレクト規則のコレクションにワイルドカード リダイレクト規則を追加します。
clear オプションの要素。

リダイレクト規則のコレクションからワイルドカード リダイレクト規則に対するすべての参照を削除します。
remove オプションの要素。

リダイレクト規則のコレクションから 1 つのワイルドカード リダイレクト規則に対する参照を削除します。

構成サンプル

HTTP リダイレクト役割サービスがインストールされると、IIS 7.0 のルート ApplicationHost.config ファイルには、次の既定の <httpRedirect> 要素が構成されます。この構成セクションは、<clear> 要素を使用している場合を除き、既定の構成設定を継承します。

<system.webServer>
   <httpRedirect enabled="false" />
</system.webServer>

次の構成サンプルでは、リダイレクトを有効にし、クライアントをリダイレクトするリダイレクト先の URL を構成します。

<system.webServer>
   <httpRedirect enabled="true" destination="https://www.contoso.com/" />

</system.webServer>

次の構成サンプルでは、ASP ファイルに対するすべての要求を Web サイトのホーム ページにリダイレクトするワイルドカード リダイレクト エントリを追加します。 : この例は、Web サイトから ASP ベースのアプリケーションをすべて削除した場合に、古いアプリケーションへのクライアント要求に対して "HTTP 404 Not Found" で応答する代わりに、これらの クライアント要求を Web サイトのルートにリダイレクトしたい場合に役立ちます。

<configuration>
   <system.webServer>

      <httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
         <add wildcard="*.php" destination="/default.htm" />
      </httpRedirect>
   </system.webServer>
</configuration>

サンプル コード

次のコード サンプルでは、HTTP 302 状態コードを使用して、すべての要求を "https://www.contoso.com" にリダイレクトするように Default Web Site を構成します。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /enabled:"True"
appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /destination:"https://www.contoso.com"
appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /exactDestination:"False"
appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /httpResponseStatus:"Found"

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("Default Web Site");

         ConfigurationSection httpRedirectSection = config.GetSection("system.webServer/httpRedirect");
         httpRedirectSection["enabled"] = true;
         httpRedirectSection["destination"] = @"https://www.contoso.com";
         httpRedirectSection["exactDestination"] = false;
         httpRedirectSection["httpResponseStatus"] = @"Found";

         serverManager.CommitChanges();
      }
   }
}

VB.NET

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

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetWebConfiguration("Default Web Site")

      Dim httpRedirectSection As ConfigurationSection = config.GetSection("system.webServer/httpRedirect")
      httpRedirectSection("enabled") = True
      httpRedirectSection("destination") = "https://www.contoso.com"
      httpRedirectSection("exactDestination") = False
      httpRedirectSection("httpResponseStatus") = "Found"

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

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

var httpRedirectSection = adminManager.GetAdminSection("system.webServer/httpRedirect", "MACHINE/WEBROOT/APPHOST/Default Web Site");
httpRedirectSection.Properties.Item("enabled").Value = true;
httpRedirectSection.Properties.Item("destination").Value = "https://www.contoso.com";
httpRedirectSection.Properties.Item("exactDestination").Value = false;
httpRedirectSection.Properties.Item("httpResponseStatus").Value = "Found";

adminManager.CommitChanges();

VBScript

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

Set httpRedirectSection = adminManager.GetAdminSection("system.webServer/httpRedirect", "MACHINE/WEBROOT/APPHOST/Default Web Site")
httpRedirectSection.Properties.Item("enabled").Value = True
httpRedirectSection.Properties.Item("destination").Value = "https://www.contoso.com"
httpRedirectSection.Properties.Item("exactDestination").Value = False
httpRedirectSection.Properties.Item("httpResponseStatus").Value = "Found"

adminManager.CommitChanges()

次のコード サンプルでは、ASP ファイルのすべての要求を Web サイトのホーム ページにリダイレクトするワイルドカード リダイレクト エントリを追加します。 : この例は、Web サイトから ASP ベースのアプリケーションをすべて削除した場合に、古いアプリケーションへのクライアント要求に対して "HTTP 404 Not Found" で応答する代わりに、これらの クライアント要求を Web サイトのルートにリダイレクトしたい場合に役立ちます。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /enabled:"True" /exactDestination:"True" /httpResponseStatus:"Found"

appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /+"[wildcard='*.asp',destination='/default.htm']"

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("Default Web Site");

         ConfigurationSection httpRedirectSection = config.GetSection("system.webServer/httpRedirect");
         httpRedirectSection["enabled"] = true;
         httpRedirectSection["exactDestination"] = true;
         httpRedirectSection["httpResponseStatus"] = @"Found";

         ConfigurationElementCollection httpRedirectCollection = httpRedirectSection.GetCollection();
         ConfigurationElement addElement = httpRedirectCollection.CreateElement("add");
         addElement["wildcard"] = @"*.asp";
         addElement["destination"] = @"/default.htm";
         httpRedirectCollection.Add(addElement);

         serverManager.CommitChanges();
      }
   }
}

VB.NET

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

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetWebConfiguration("Default Web Site")

      Dim httpRedirectSection As ConfigurationSection = config.GetSection("system.webServer/httpRedirect")
      httpRedirectSection("enabled") = True
      httpRedirectSection("exactDestination") = True
      httpRedirectSection("httpResponseStatus") = "Found"

      Dim httpRedirectCollection As ConfigurationElementCollection = httpRedirectSection.GetCollection
      Dim addElement As ConfigurationElement = httpRedirectCollection.CreateElement("add")
      addElement("wildcard") = "*.asp"
      addElement("destination") = "/default.htm"
      httpRedirectCollection.Add(addElement)

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

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

var httpRedirectSection = adminManager.GetAdminSection("system.webServer/httpRedirect", "MACHINE/WEBROOT/APPHOST/Default Web Site");
httpRedirectSection.Properties.Item("enabled").Value = true;
httpRedirectSection.Properties.Item("exactDestination").Value = true;
httpRedirectSection.Properties.Item("httpResponseStatus").Value = "Found";

var httpRedirectCollection = httpRedirectSection.Collection;
var addElement = httpRedirectCollection.CreateNewElement("add");
addElement.Properties.Item("wildcard").Value = "*.asp";
addElement.Properties.Item("destination").Value = "/default.htm";
httpRedirectCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

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

Set httpRedirectSection = adminManager.GetAdminSection("system.webServer/httpRedirect", "MACHINE/WEBROOT/APPHOST/Default Web Site")
httpRedirectSection.Properties.Item("enabled").Value = True
httpRedirectSection.Properties.Item("exactDestination").Value = True
httpRedirectSection.Properties.Item("httpResponseStatus").Value = "Found"

Set httpRedirectCollection = httpRedirectSection.Collection
Set addElement = httpRedirectCollection.CreateNewElement("add")
addElement.Properties.Item("wildcard").Value = "*.asp"
addElement.Properties.Item("destination").Value = "/default.htm"
httpRedirectCollection.AddElement(addElement)

adminManager.CommitChanges()