リダイレクト ヘッダー <redirectHeaders>

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

概要

<redirectHeaders> 要素は、インターネット インフォメーション サービス (IIS) 7.0 が HTTP リダイレクトに追加するカスタム HTTP ヘッダーのコレクションを指定します。

: HTTP ヘッダーは、Web サーバーからの応答で返される名前と値のペアです。Web サーバーからのすべての応答で返されるカスタム ヘッダーとは異なり、リダイレクト ヘッダーはリダイレクトが発生した場合にのみ返されます。

互換性

  IIS 7.0 IIS 6.0
説明 <httpProtocol><redirectHeaders> は IIS 7.0 で新たに導入された要素です。 なし

セットアップ

<httpProtocol> 要素の <redirectHeaders> 要素は、IIS 7.0 の既定のインストールに含まれています。

方法

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

構成

属性

なし。

子要素

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

<redirectHeaders> コレクションに応答ヘッダーを追加します。
clear オプションの要素。

<redirectHeaders> コレクションからすべての応答ヘッダーの参照を削除します。
remove オプションの要素。

<redirectHeaders> コレクションから 1 つの応答ヘッダーの参照を削除します。

構成サンプル

次の構成サンプルでは、IIS 7.0 が要求をリダイレクトしたときにのみ応答に追加されるカスタム HTTP ヘッダーおよび値を指定します。

<configuration>
   <system.webServer>
      <httpProtocol>

         <redirectHeaders>
            <add name="X-Custom-Redirect-Header" value="MyRedirectValue" />
         </redirectHeaders>
      </httpProtocol>
   </system.webServer>
</configuration>

: IIS 7.0 の ApplicationHost.config ファイルには、次の既定の <httpProtocol> 要素が構成されています。

<httpProtocol>
   <customHeaders>
      <clear />

      <add name="X-Powered-By" value="ASP.NET" />
   </customHeaders>
   <redirectHeaders>
      <clear />
   </redirectHeaders>
</httpProtocol>

サンプル コード

次のコード サンプルでは、IIS 7.0 が要求をリダイレクトしたときにのみ応答に追加されるカスタム HTTP ヘッダーおよび値を指定します。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/httpProtocol /+"redirectHeaders.[name='X-Custom-Redirect-Header',value='MyRedirectValue']" 

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 httpProtocolSection = config.GetSection("system.webServer/httpProtocol");
         ConfigurationElementCollection redirectHeadersCollection = httpProtocolSection.GetCollection("redirectHeaders");

         ConfigurationElement addElement = redirectHeadersCollection.CreateElement("add");
         addElement["name"] = @"X-Custom-Redirect-Header";
         addElement["value"] = @"MyRedirectValue";
         redirectHeadersCollection.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 httpProtocolSection As ConfigurationSection = config.GetSection("system.webServer/httpProtocol")
      Dim redirectHeadersCollection As ConfigurationElementCollection = httpProtocolSection.GetCollection("redirectHeaders")

      Dim addElement As ConfigurationElement = redirectHeadersCollection.CreateElement("add")
      addElement("name") = "X-Custom-Redirect-Header"
      addElement("value") = "MyRedirectValue"
      redirectHeadersCollection.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 httpProtocolSection = adminManager.GetAdminSection("system.webServer/httpProtocol", "MACHINE/WEBROOT/APPHOST/Default Web Site");
var redirectHeadersCollection = httpProtocolSection.ChildElements.Item("redirectHeaders").Collection;

var addElement = redirectHeadersCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "X-Custom-Redirect-Header";
addElement.Properties.Item("value").Value = "MyRedirectValue";
redirectHeadersCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"
Set httpProtocolSection = adminManager.GetAdminSection("system.webServer/httpProtocol", "MACHINE/WEBROOT/APPHOST/Default Web Site")
Set redirectHeadersCollection = httpProtocolSection.ChildElements.Item("redirectHeaders").Collection

Set addElement = redirectHeadersCollection.CreateNewElement("add")
addElement.Properties.Item("name").Value = "X-Custom-Redirect-Header"
addElement.Properties.Item("value").Value = "MyRedirectValue"
redirectHeadersCollection.AddElement(addElement)

adminManager.CommitChanges()