简体   繁体   中英

Customize HTTP header in IIS URL Rewrite module

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 http://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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM