11

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

  • 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

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

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.

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, …)

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>
Michael
  • 8,362
  • 6
  • 61
  • 88
user80498
  • 735
  • 1
  • 7
  • 15

1 Answers1

31

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

  • custom headers need to be preceded by "HTTP_".
  • substitute dashes with underscores

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>

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

user80498
  • 735
  • 1
  • 7
  • 15
  • According to the documentation, you can use `HEADER_x-app-version`. when using IIS newer than 5.1. (I used `pattern=".*"` to perform a simple check for a custom header.) – James Moberg Jan 18 '22 at 18:04
  • Actually, the pattern to verify the existence of a custom header should be `.+` (one or more) instead of `.*` (zero or more). – James Moberg Jan 31 '22 at 19:55