基于自定义HTTP标头的UrlRewrite条件 [英] UrlRewrite condition based on custom HTTP header

查看:65
本文介绍了基于自定义HTTP标头的UrlRewrite条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在具有2个条件的UrlRewrite中配置一条规则:

I'm trying to configure a rule in UrlRewrite that has 2 conditions:

  • HTTP标头 HTTP_HOST 需要匹配某个域(api.contoso.com)
  • HTTP请求中必须存在一个自定义HTTP标头 x-app-version
  • HTTP header HTTP_HOST needs to match a certain domain (api.contoso.com)
  • A custom HTTP header x-app-version needs to be present in the HTTP request

根据此信息,我想在后端重定向到该API的其他版本.

based on this info, I'd like to redirect to a different version of the API in the back-end.

问题该规则在第一个条件下表现正常.当 HTTP_HOST 与api.contoso.com匹配时,它将启动.但是,它会忽略我的自定义 x-app-version 标头.

Problem The rule is behaving as expected on the first condition. It kicks in when HTTP_HOST matches api.contoso.com. However, it ignores my custom x-app-version header.

假设因此,我担心只能与下拉列表中有限的预定义HTTP标头集( HTTP_HOST REMOTE_ADDRESS REMOTE_HOST ,…)

Assumption So, I fear that a UrlRewrite condition only can be defined in combination with the limited set of predefined HTTP headers from the dropdown (HTTP_HOST, REMOTE_ADDRESS, REMOTE_HOST, …)

问题假设是正确的还是应该可行?我的配置或其他方法是否存在基于自定义定义的HTTP标头的条件的错误?

Question Is assumption correct or should this be possible whatsoever? Is there a mistake in my config or other approach to have conditions based on a custom defined HTTP header?

<rule name="ARR_test2" enabled="false">
  <match url="(.*)" />  
  <conditions>
    <add input="{HTTP_HOST}" pattern="api.contoso.com" />
    <add input="{x-app-version}" pattern="1" />
  </conditions>
  <action type="Rewrite" url="https://FARM_apiv1/{R:0}" />
</rule>

推荐答案

好,我发现了如何在UrlRewrite条件下使用自定义HTTP标头:

Ok, I found out how to use custom HTTP headers in a UrlRewrite condition:

  • 自定义标头必须以"HTTP_"开头.
  • 用下划线代替破折号

例如:为了检索自定义标头"x-app-version",我可以使用"HTTP_x_app_version".所以我的UrlRewrite配置应该像这样

E.g.: in order to retrieve my custom header "x-app-version", I can use "HTTP_x_app_version". So my UrlRewrite config should look like this

<rule name="ARR_test2" enabled="false">
  <match url="(.*)" />  
  <conditions>
    <add input="{HTTP_HOST}" pattern="api.contoso.com" />
    <add input="{HTTP_x_app_version}" pattern="1" />
  </conditions>
  <action type="Rewrite" url="https://FARM_apiv1/{R:0}" />
</rule>

在文档中实际上已经很清楚地提到了它: https://msdn.microsoft.com/zh-us/library/ms524602(v = vs.90).aspx

It's actually mentioned quite clear in the documentation: https://msdn.microsoft.com/en-us/library/ms524602(v=vs.90).aspx

这篇关于基于自定义HTTP标头的UrlRewrite条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆