Capturing value in cookie using URL rewriting for IIS7

I need to write a URL rewrite rule for my IIS 7.5 website that captures a value in a specific cookie and then uses that value to create a URL. For example, incoming requests are as follows:

GET http://myserver.com/test.aspx HTTP/1.1 Accept: text/html, application/xhtml+xml, */* Accept-Language: en-US User-Agent: Mozilla/5.0 Host: myserver.com Cookie: foo=bar; bat=bar 

I would like to direct them to this (based on the cookie value "foo"):

 http://myserver.com/bar/test.aspx 

Revision of documentation and search for examples, I'm at a standstill! Thank you for your help.

+7
source share
2 answers

Answering my own question, here is a working example. The template may require additional work depending on which characters require support, but the following rule will use the detected cookie value and the route to the discovered server - and the server can be specified by IPv4 address or by name (alphanumeric and period).

 <rule name="Route Base On Cookie" stopProcessing="true"> <match url="^(.*)" /> <conditions> <add input="{HTTP_COOKIE}" pattern="foo=([0-9.a-zA-Z]+)" /> </conditions> <action type="Rewrite" url="http://{C:1}/{R:0}" /> </rule> 
+9
source

@Geoffrey For your code support to return a cookie value, I would recommend this template:

 <add input="{HTTP_COOKIE}" pattern="foo=(.*?);" /> 

As a reminder, the value of {HTTP_COOKIE} looks like this:

Cookie: foo = myexamplevalue; expires = Wed, 03-May-2014 22:31:08 GMT; Path = /; HttpOnly \ r \ n

+1
source

All Articles