Adding Redirect Headers <add>


The <add> element of the <redirectHeaders> element adds an HTTP response header to the 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.


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


The <add> element of the <redirectHeaders> 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.



Attribute Description
name Required string attribute.

Specifies a field name for the redirect header. In a response, a field name precedes the related field value.
Value Optional string attribute.

Specifies a field value for the redirect header. In a response, a field value follows the related field name.

Child Elements


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.

            <add name="X-Custom-Redirect-Header" value="MyRedirectValue" />

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

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

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 set config "Default Web Site" -section:system.webServer/httpProtocol /+"redirectHeaders.[name='X-Custom-Redirect-Header',value='MyRedirectValue']" 


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";



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"

   End Sub

End Module


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";



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"

Deprecated Elements