根据文件类型添加自定义标题 [英] Add custom header based on file type
问题描述
我们正在尝试为 IIS 7.5 中的站点地图文件添加自定义标头 (X-Robots-Tag).但是,IIS 似乎不支持基于文件类型或通配符(仅子文件夹)的自定义标头.
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).
我们可以通过 Web.config 为 *.xml.gz 文件添加自定义标头吗?
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.
推荐答案
您可以使用 IIS UrlRewrite 模块 并添加自定义出站规则以配置自定义标头.以下是您可能想要使用的示例规则:
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>
更多信息:UrlRewrite 文档
这篇关于根据文件类型添加自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!