Add custom header based on file type
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
Related videos on Youtube
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, 2022Comments
-
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 almost 12 yearsIs 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 almost 9 yearsI use
REQUEST_FILENAME
instead ofREQUEST_URI
-
user2173353 about 7 yearsSo, if you want to change the
Cache-Control
header, what should you use? Is there a list of server variables somewhere that containsRESPONSE_X_Robots_Tag
? How does IIS know thatRESPONSE_X_Robots_Tag
relates to theX-Robots-Tag
header? -
user2173353 about 7 yearsHm... I see that
RESPONSE_Cache_Control
is working in order to setCache-Control
! Interesting...