Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlRewrite condition based on custom HTTP header

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>
like image 529
user80498 Avatar asked Mar 27 '17 15:03

user80498


1 Answers

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

like image 53
user80498 Avatar answered Sep 24 '22 09:09

user80498