Add custom header based on file type

14,759

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

Share:
14,759

Related videos on Youtube

Matt Beckman
Author by

Matt Beckman

I am a software engineer who enjoys building clean, consistent, scalable, and highly testable code using Test-Driven and Agile Development methods on .NET, Node.js, Aurelia and other open-source platforms. I currently prefer serverless architecture on the AWS stack (Lambda, Docker), and I'm fairly active in the blockchain/cryptocurrency community, as part of the App/UI team for the BitShares decentralized exchange (DEX), among other projects. I am also an experienced systems administrator who has been responsible for managing large Windows/Linux networks, building scalable platforms for high-traffic websites, and utilizing my engineering background to build automated deployment, testing, and QA systems. Preferred tech: Node.js, Aurelia, React, Docker, AWS, PHP, .NET

Updated on June 22, 2022

Comments

  • Matt Beckman
    Matt Beckman almost 2 years

    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.

  • Lonnie Best
    Lonnie Best almost 12 years
    Is there a way to make the value dynamic like today's date plus 7 days, if so, post here: stackoverflow.com/questions/10825497/…
  • Bernhard Döbler
    Bernhard Döbler almost 9 years
    I use REQUEST_FILENAME instead of REQUEST_URI
  • user2173353
    user2173353 about 7 years
    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
    user2173353 about 7 years
    Hm... I see that RESPONSE_Cache_Control is working in order to set Cache-Control! Interesting...