Redirect Headers <redirectHeaders>

Overview

The <redirectHeaders> element specifies a collection of custom HTTP headers that Internet Information Services (IIS) 7 will add to HTTP redirects.

Note

HTTP headers are name and value pairs that are returned in responses from a Web server. Unlike custom headers, which are returned in every response from a Web server, redirect headers are returned only when redirection occurs.

Compatibility

Version Notes
IIS 10.0 The <redirectHeaders> element was not modified in IIS 10.0.
IIS 8.5 The <redirectHeaders> element was not modified in IIS 8.5.
IIS 8.0 The <redirectHeaders> element was not modified in IIS 8.0.
IIS 7.5 The <redirectHeaders> element was not modified in IIS 7.5.
IIS 7.0 The <redirectHeaders> element of the <httpProtocol> element was introduced in IIS 7.0.
IIS 6.0 N/A

Setup

The <redirectHeaders> element of the <httpProtocol> element is included in the default installation of IIS 7.

How To

There is no user interface for adding values to the <redirectHeaders> element for IIS 7. For examples of how to add values to the <redirectHeaders> element programmatically, see the Code Samples section of this document.

Configuration

Attributes

None.

Child Elements

Element Description
add Optional element.

Adds a response header to the <redirectHeaders> collection.
clear Optional element.

Removes all references to response headers from the <redirectHeaders> collection.
remove Optional element.

Removes a reference to a response header from the <redirectHeaders> collection.

Configuration Sample

The following configuration sample specifies a custom HTTP header and value that will only be added to the response when IIS 7 redirects a request.

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

Note

The following default <httpProtocol> element is configured in the ApplicationHost.config file in IIS 7.

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

Sample Code

The following code samples specify a custom HTTP header and value that will only be added to the response when IIS 7 redirects a request.

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()