- 概要
- 互換性
- セットアップ
- 方法
- 構成
- サンプル コード
※本ページに挿入されている画像をクリックすると、画像全体が別ウィンドウで表示されます。
<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 の既定のインストールには含まれていません。インストールするには、次の手順を実行します。
タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [サーバー マネージャー] をクリックします。
[サーバー マネージャー] ウィンドウのツリー表示で、[役割] を展開して [Web サーバー (IIS)] をクリックします。
[Web サーバー (IIS)] ウィンドウで、[役割サービス] セクションまでスクロールして [役割サービスの追加] をクリックします。
役割サービスの追加ウィザードの [役割サービスの選択] ページで、[HTTP 共通機能] を展開し、[HTTP リダイレクト] を選択して、[次へ] をクリックします。
[インストール オプションの確認] ページで [インストール] をクリックします。
[結果] ページで [閉じる] をクリックします。
タスク バーで [スタート] ボタンをクリックし、[コントロール パネル] をクリックします。
コントロール パネルで、[プログラムと機能]、[Windows の機能の有効化または無効化] の順にクリックします。
[Internet Information Services]、[World Wide Web サービス]、[HTTP 共通機能] の順に展開します。
[HTTP リダイレクト] を選択して、[OK] をクリックします。
IIS 7.0 で HTTP ワイルドカード リダイレクトを追加するためのユーザー インターフェイスはありません。プログラムによって <httpRedirect>
要素に <add>
要素を追加する方法の例については、このドキュメントの「サンプル コード」セクションを参照してください。
タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。
[接続] ウィンドウで、当該サーバー名を展開して [サイト] を展開し、カスタム エラー ページを構成する Web サイトまたは Web アプリケーションを選択します。
[ホーム] ウィンドウで [HTTP リダイレクト] をダブルクリックします。
[HTTP リダイレクト] ウィンドウで、要求をリダイレクトするチェック ボックスをオンにして、リダイレクト先の URL を入力します。
オプションで、次のいずれかを指定できます。
- リダイレクト先を入力したとおりに構成します。
- リダイレクト先を 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
これらの変更をすべて完了したら、[操作] ウィンドウで [適用] をクリックします。
属性 | 説明 |
---|---|
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 説明:
値: Permanent 説明:
値: Temporary 説明:
|
要素 | 説明 |
---|---|
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 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"
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();
}
}
}
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
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();
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 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']"
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();
}
}
}
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
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();
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()