开发者

Customize HTTP header in IIS URL Rewrite module

开发者 https://www.devze.com 2023-04-12 08:25 出处:网络
I was stuck by a simple outbound rule, I want to modify the HTTP Content-Type to application/atom+xml, if the URL exactly matches ht开发者_StackOverflow中文版tp://wayneye.com/Feeds/Atom

I was stuck by a simple outbound rule, I want to modify the HTTP Content-Type to application/atom+xml, if the URL exactly matches ht开发者_StackOverflow中文版tp://wayneye.com/Feeds/Atom

My rule XML:

<outboundRules>
<rule name="AtomFeedsIMEType" patternSyntax="ExactMatch">
    <match serverVariable="RESPONSE_CONTENT_TYPE" pattern="http://{HTTP_HOST}/Feeds/Atom" />
    <action type="Rewrite" value="application/atom+xml" />
</rule>

Need help...


You are matching the server variable against the full URL, including domain name. That's not going to work ;-). It doesn't really matter what the value of the Content-Type is, you're going to replace it anyway so you can match is against anything. To make sure you don't replace it on every page, you need to add a precondition to match only requests starting with /Feeds/Atom (on {REQUEST_URI} ). Here's an example:

<outboundRules>
  <rule name="AtomFeedsIMEType" preCondition="Match atom feeds">
    <match serverVariable="RESPONSE_Content_Type" pattern="(.*)" negate="false" />
    <action type="Rewrite" value="application/atom+xml" replace="true" />
  </rule>
  <preConditions>
    <preCondition name="Match atom feeds">
      <add input="{REQUEST_URI}" pattern="^/Feeds/Atom" />
    </preCondition>
  </preConditions>
</outboundRules>

For this to work, the server has to be set up to allow changing of the Content-Type header. This can be done either on the server level or on the site level but needs to be done by the Administrator. It's set in the applicationHost.config and not in the web.config. Here is a part of the applicationHost.config that allows that:

<location path="your_site_name">
  <system.webServer>
    <rewrite>
      <allowedServerVariables>
        <add name="CONTENT_TYPE" />
      </allowedServerVariables>
    </rewrite>
  </system.webServer>
</location>

You can also allow this from the GUI, with the View Server Variables link under actions from the main URLRewrite screen. Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消