简体   繁体   English

在IIS URL重写模块中自定义HTTP标头

[英]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 我被一个简单的出站规则所困,如果URL与http://wayneye.com/Feeds/Atom完全匹配,我想将HTTP Content-Type修改为application / atom + xml

My rule XML: 我的规则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. 您将服务器变量与完整URL匹配,包括域名。 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. Content-Type的价值是什么并不重要,无论如何你都要替换它,所以你可以匹配任何东西。 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} ). 要确保不在每个页面上替换它,您需要添加前提条件以仅匹配以/ Feeds / Atom开头的请求(在{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. 为此,必须设置服务器以允许更改Content-Type标头。 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. 它在applicationHost.config中设置,而不是在web.config中设置。 Here is a part of the applicationHost.config that allows that: 这是applicationHost.config的一部分,它允许:

<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. 您也可以从GUI允许此操作,在主URLRewrite屏幕的操作下使用View Server Variables链接。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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