29

We are trying to add a custom header (X-Robots-Tag) for sitemap files in IIS 7.5. However, it does not appear that IIS supports custom headers based on a file type or wildcard (only subfolders).

Can we add a custom header for only *.xml.gz files via Web.config?

We would like to avoid making the customization via code or on our load balancer.

Matt Beckman
  • 5,022
  • 4
  • 29
  • 42

1 Answers1

49

You can use the IIS UrlRewrite module and add a custom outbound rule to configure the custom header. Here is a sample rule you may want to use:

  <system.webServer>
    <rewrite>
      <outboundRules>
        <rule name="Set custom HTTP response header">
          <match serverVariable="RESPONSE_X_Robots_Tag" pattern=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="\.xml\.gz$" />
          </conditions>
          <action type="Rewrite" value="The value you need for this header"/>
        </rule>
      </outboundRules>
    </rewrite>
  </system.webServer>

More info: UrlRewrite documentation

Melodron
  • 123
  • 1
  • 10
Efran Cobisi
  • 6,138
  • 22
  • 22
  • Is there a way to make the value dynamic like today's date plus 7 days, if so, post here: http://stackoverflow.com/questions/10825497/iis-7-5-how-do-you-add-a-dynamic-http-expires-header – Lonnie Best May 31 '12 at 05:19
  • I use `REQUEST_FILENAME` instead of `REQUEST_URI` – Bernhard Döbler Jun 01 '15 at 15:10
  • 5
    So, if you want to change the `Cache-Control` header, what should you use? Is there a list of server variables somewhere that contains `RESPONSE_X_Robots_Tag`? How does IIS know that `RESPONSE_X_Robots_Tag` relates to the `X-Robots-Tag` header? – user2173353 Mar 16 '17 at 12:48
  • 1
    Hm... I see that `RESPONSE_Cache_Control` is working in order to set `Cache-Control`! Interesting... – user2173353 Mar 16 '17 at 13:15