Share via


HTTP リダイレクト: <httpRedirect>

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

クライアントを新しい場所にリダイレクトするにはいくつかの理由があります。 たとえば、会社が新しい Web サイトに移行する場合、古い Web サイトから新しい Web サイトにすべての要求をリダイレクトできます。 同様に、Web サーバーに新しいアプリケーションをデプロイした場合、古いアプリケーションの URL 名前空間 (http://www.contoso.com/app_v1.0/ など) に対するすべての要求を、新しいアプリケーションの場所 (http://www.contoso.com/app_v2.0/ など) にリダイレクトできます。

最も簡単な構成で、クライアントを新しい場所にリダイレクトするには、<httpRedirect> 要素の enableddestination 属性を設定することだけが必要です。 ただし、exactDestinationhttpResponseStatus 属性などの追加の要素を使って、それぞれ、IIS 7 が入力されたとおりに宛先 URL を返すかどうか、および Web クライアントに返す HTTP 応答コードを指定して、リダイレクトのエンド ユーザー エクスペリエンスを構成できます。

互換性

バージョン メモ
IIS 10.0 IIS 10.0 では、<httpRedirect> 要素に新しい HTTP 応答状態が追加されました。
IIS 8.5 <httpRedirect> 要素は、IIS 8.5 では変更されませんでした。
IIS 8.0 <httpRedirect> 要素は、IIS 8.0 では変更されませんでした。
IIS 7.5 <httpRedirect> 要素は、IIS 7.5 では変更されませんでした。
IIS 7.0 <httpRedirect> 要素は IIS 7.0 で導入されました。
IIS 6.0 <httpRedirect> 要素は、IIS 6.0 の HttpRedirect メタベース プロパティを置き換えます。

段取り

HTTP リダイレクトは、IIS 7 以降の既定のインストールでは使用できません。 インストールするには、次の手順を使用します。

Windows Server 2012 または Windows Server 2012 R2

  1. タスク バーで [サーバー マネージャー]をクリックします。
  2. [サーバー マネージャー] で、[管理] メニューをクリックし、[役割と機能の追加] をクリックします。
  3. 役割と機能の追加ウィザードで、[次へ] をクリックします。 インストールの種類を選択し、[次へ] をクリックします。 対象サーバーを選択し、[次へ] をクリックします。
  4. [サーバー ロール] ページで、[Web サーバー (IIS)][Web サーバー][HTTP 共通機能] の順に展開し、[HTTP リダイレクト] を選びます。 次へ をクリックします。
    Screenshot of the Server Roles page. H T T P Redirection is selected and highlighted.
  5. [機能の選択] ページで、[次へ] をクリックします。
  6. [インストール オプションの確認] ページで、[インストール] をクリックします。
  7. [結果] ページで、 [閉じる]をクリックします。

Windows 8 または Windows 8.1

  1. [スタート] 画面で、ポインターを左下隅まで移動し、[スタート] ボタンを右クリックし、[コントロール パネル] をクリックします。
  2. [コントロール パネル][プログラムと機能] をクリックし、[Windows の機能の有効化または無効化] をクリックします。
  3. [インターネット インフォメーション サービス][World Wide Web サービス][HTTP 共通機能] の順に展開して、[HTTP リダイレクト] を選びます。
    Screenshot of the Internet Information Services navigation tree. The World Wide Web Services option is expanded. Common H T T P Features is expanded and H T T P Redirection is selected.
  4. OK をクリックします。
  5. 閉じるをクリックします。

Windows Server 2008 または Windows Server 2008 R2

  1. タスク バーの [スタート] をクリックし、[管理ツール] をポイントして、[サーバー マネージャー] をクリックします。
  2. [サーバー マネージャー] 階層ウィンドウで [役割] を展開し、[Web サーバー (IIS)] をクリックします。
  3. [Web サーバー (IIS)] ウィンドウで、[役割サービス] セクションまでスクロールし、[役割サービスの追加] をクリックします。
  4. 役割サービスの追加ウィザード の [役割サービスの選択] ページで、[HTTP 共通機能] を展開し、[HTTP リダイレクト] を選んで、[次へ] をクリックします。
    Screenshot of the Role Services section. H T T P Redirection is selected and highlighted.
  5. [インストール オプションの確認] ページで、[インストール] をクリックします。
  6. [結果] ページで、 [閉じる]をクリックします。

Windows Vista または Windows 7

  1. タスク バーで、[スタート][コントロール パネル] の順にクリックします。
  2. [コントロール パネル][プログラムと機能] をクリックし、[Windows の機能の有効化または無効化] をクリックします。
  3. [インターネット インフォメーション サービス][World Wide Web サービス][HTTP 共通機能] の順に展開します。
  4. [HTTP リダイレクト] を選んで、[OK] をクリックします。
    Screenshot of the Programs and Features navigation tree. H T T P Redirection is selected and highlighted.

操作方法

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

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

  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. [ホーム] ウィンドウで、[HTTP リダイレクト] をダブルクリックします。
    Screenshot of the Home pane. The H T T P Redirect icon is highlighted and selected.

  4. [HTTP リダイレクト] ウィンドウで、要求をリダイレクトするボックスをオンにして、宛先 URL を入力します。
    Screenshot of the H T T P Redirect pane. The box next to Redirect requests to this destination is checked.

  5. 必要に応じて、次のいずれかのオプションを指定できます。

    • リダイレクト先を入力された正確な宛先として構成します。

    • リダイレクト先を、サブフォルダーではなく、宛先 URL のルート フォルダーに制限するように構成します。

    • HTTP 状態コードを構成します。次のいずれかのオプションを使用できます。

      • 301 永続的
      • 302 検出
      • 307 一時的
      • 308 永続的なリダイレクト

      Note

      上の各オプションに対し、IIS 7 からは、それぞれ、次の実際の HTTP 応答状態が返されます。

      • HTTP/1.1 301 永続的に移動
      • HTTP/1.1 302 リダイレクト
      • HTTP/1.1 307 リダイレクト
      • HTTP 1.1 308 永続的にリダイレクト
  6. 上記の変更がすべて完了したら、[タスク] ウィンドウの [適用] をクリックします。

構成

属性

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

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

既定値は false です。
destination 省略可能な文字列属性。

クライアントのリダイレクト先の URL または仮想パスを指定します。
enabled 省略可能な Boolean 属性です。

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

既定値は false です。
exactDestination 省略可能な Boolean 属性です。

destination の値を、相対的な場所ではなく、絶対的なターゲットの場所と見なす必要があることを指定します。

既定値は false です。
httpResponseStatus 省略可能な列挙型属性。

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

httpResponseStatus 属性には、次のいずれかの値を指定できます。 既定値は、Found です。
Value 説明
Found 302 状態コードを返します。これは、destination 属性で指定されている場所に新しい要求を発行するようクライアントに指示します。

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

この数値は 301 です。
Temporary 307 状態コードを返します。これは、ブラウザーが HTTP POST 要求を発行したときにクライアントのデータが失われるのを防ぎます。

この数値は 307 です。
PermRedirect 308 状態コードを返します。これは、要求されたリソースの場所が完全に変更されたことをクライアントに通知します。

この数値は 308 です。

子要素

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

リダイレクト規則のコレクションにワイルドカード リダイレクト規則を追加します。
clear 省略可能な要素です。

リダイレクト規則のコレクションから、ワイルドカード リダイレクト規則へのすべての参照を削除します。
remove 省略可能な要素です。

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

構成サンプル

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

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

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

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

次の構成サンプルでは、PHP ファイルに対するすべての要求を Web サイトのホーム ページにリダイレクトするワイルドカード リダイレクト エントリが追加されます。

Note

この例は、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 状態コードを使ってすべての要求を http://www.contoso.com にリダイレクトするように、既定の Web サイトを構成します。

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:"http://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"] = @"http://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") = "http://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 = "http://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 = "http://www.contoso.com"
httpRedirectSection.Properties.Item("exactDestination").Value = False
httpRedirectSection.Properties.Item("httpResponseStatus").Value = "Found"

adminManager.CommitChanges()

次のコード サンプルでは、ASP ファイルに対するすべての要求を Web サイトのホーム ページにリダイレクトするワイルドカード リダイレクト エントリが追加されます。

Note

この例は、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()