Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 573
Wayne Ye Avatar asked Oct 11 '11 08:10

Wayne Ye


People also ask

How do you modify the HTTP headers of the Web server?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

How do I change the response header in IIS?

In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name. In the Value box, type the custom HTTP header value.

What is IIS URL Rewrite Module?

The Microsoft URL Rewrite Module 2.0 for IIS 7 and above enables IIS administrators to create powerful customized rules to map request URLs to friendly URLs that are easier for users to remember and easier for search engines to find.


1 Answers

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.

like image 146
Marco Miltenburg Avatar answered Sep 29 '22 11:09

Marco Miltenburg