HTTP ワイルドカード リダイレクトの追加: <add>
概要
<httpRedirect>
要素の <add>
要素は、リダイレクト規則のコレクションにワイルドカード リダイレクト規則を追加します。 ワイルドカード規則を使うと、コンテンツのグループに対するリダイレクト規則を追加できます。 たとえば、*.aspx ファイル名拡張子を使っている .NET ページにアプリケーションを移行した後で、すべての *.asp ページを削除する場合、古い ASP ベースのアプリケーションに対するすべての *.asp 要求を Web サイトのホーム ページにリダイレクトする、ワイルドカード リダイレクト規則を追加できます。
Note
ワイルドカード リダイレクト規則を追加する場合、ワイルドカード規則を機能させるには、<httpRedirect>
セクションで既定の宛先の値を削除する必要があります。
互換性
バージョン | メモ |
---|---|
IIS 10.0 | <add> 要素は IIS 10.0 では変更されませんでした。 |
IIS 8.5 | <add> 要素は IIS 8.5 では変更されませんでした。 |
IIS 8.0 | <add> 要素は IIS 8.0 では変更されませんでした。 |
IIS 7.5 | <add> 要素は IIS 7.5 では変更されませんでした。 |
IIS 7.0 | <httpRedirect> 要素の <add> 要素が IIS 7.0 で導入されました。 |
IIS 6.0 | 該当なし |
段取り
HTTP リダイレクトは、IIS 7 以降の既定のインストールでは使用できません。 インストールするには、次の手順を使用します。
Windows Server 2012 または Windows Server 2012 R2
- タスク バーで [サーバー マネージャー]をクリックします。
- [サーバー マネージャー] で、[管理] メニューを選択し、[役割と機能の追加] を選択します。
- [役割と機能の追加] ウィザードで、[次へ] をクリックします。 インストールの種類を選択し、[次へ] をクリックします。 対象サーバーを選び、[次へ] をクリックします。
- [サーバー ロール] ページで、[Web サーバー (IIS)]、[Web サーバー]、[HTTP 共通機能] の順に展開し、[HTTP リダイレクト] を選びます。 次へ をクリックします。
.
- [機能の選択] ページで、[次へ] をクリックします。
- [インストール オプションの確認] ページで、[インストール] をクリックします。
- [結果] ページで、 [閉じる]をクリックします。
Windows 8 または Windows 8.1
- [スタート] 画面で、ポインターを左下隅まで移動し、[スタート] ボタンを右クリックし、[コントロール パネル] をクリックします。
- [コントロール パネル] で [プログラムと機能] をクリックし、[Windows の機能の有効化または無効化] をクリックします。
- [インターネット インフォメーション サービス]、[World Wide Web サービス]、[HTTP 共通機能] の順に展開して、[HTTP リダイレクト] を選びます。
- OK をクリックします。
- 閉じるをクリックします。
Windows Server 2008 または Windows Server 2008 R2
- タスク バーで [スタート] をクリックし、[管理ツール] をポイントして、[サーバー マネージャ] をクリックします。
- [サーバー マネージャ] 階層ウィンドウで [役割] を展開し、[Web サーバー (IIS)] をクリックします。
- [Web サーバー (IIS)] ウィンドウで、[役割サービス] セクションまでスクロールし、[役割サービスの追加] をクリックします。
- 役割サービスの追加ウィザード の [役割サービスの選択] ページで、[HTTP 共通機能] を展開し、[HTTP リダイレクト] を選んで、[次へ] をクリックします。
- [インストール オプションの確認] ページで、[インストール] をクリックします。
- [結果] ページで、 [閉じる]をクリックします。
Windows Vista または Windows 7
- タスク バーで、[スタート]、[コントロール パネル] の順にクリックします。
- [コントロール パネル] で [プログラムと機能] をクリックし、[Windows の機能の有効化または無効化] をクリックします。
- [インターネット インフォメーション サービス]、[World Wide Web サービス]、[HTTP 共通機能] の順に展開します。
- [HTTP リダイレクト] を選んで、[OK] をクリックします。
操作方法
IIS 7 にワイルドカード HTTP リダイレクトを追加するためのユーザー インターフェイスはありません。 プログラムで <httpRedirect>
要素に <add>
要素を追加する方法の例については、このドキュメントのコード サンプルのセクションを参照してください。
構成
属性
属性 | 説明 |
---|---|
destination |
必須の文字列属性です。 関連するワイルドカード値と一致する要求をリダイレクトする場所を指定します。 |
wildcard |
必須の文字列属性です。 要求を比較する一意のワイルドカード値を指定します。 ワイルドカード値と一致した要求は、指定した宛先にリダイレクトされます。 |
子要素
なし。
構成サンプル
次の構成サンプルでは、ASP ファイルに対するすべての要求を 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>
サンプル コード
次のコード サンプルでは、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()