Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ARR how do you rewrite a url if a cookie is missing?

Tags:

asp.net

iis

arr

I know I can rewrite a url based on values from a cookie using the {HTTP_COOKIE} variable in the conditions part of the rule. This rule grabs a cookie called ServerProxy and does a rewrite to that server url.

<rule name="SendTrafficToServerProxyCookieValue" stopProcessing="true">
    <match url="(.*)" />
    <action type="Rewrite" url="http://{C:1}/{R:0}" />
    <conditions>
        <add input="{HTTP_COOKIE}" pattern="ServerProxy=(.*)" />
    </conditions>
</rule>

If the ServerProxy cookie is absent or unset I would like to direct the traffic to an authentication server called authenticate.app. How do I write a rewrite rule that will do that?

like image 309
Aran Mulholland Avatar asked Aug 14 '15 06:08

Aran Mulholland


People also ask

Where is URL rewrite in IIS?

After installation, you will find the URL Rewrite option under the HTTP features section in IIS settings. Now, you can add own rewrite rules. This also allows importing rules like from . htaccess.

Where are IIS rewrite rules stored?

When done on the server level it is saved in the ApplicationHost. config file. You can also define it on the folder level, it that case it is saved in a web. config file inside that folder.

What is inbound and outbound rules in IIS?

There are two rule types, inbound and outbound. Inbound rules look at the request URLs and change them. Outbound rules inspect the traffic sent out, look for URLs within it, and rewrite them as needed.


1 Answers

Try this:

<rule name="SendTrafficToServerProxyCookieValue" stopProcessing="true">
    <match url="(.*)" />
    <action type="Rewrite" url="http://{C:1}/{R:0}" />
    <conditions>
        <add input="{HTTP_COOKIE}" pattern="ServerProxy=(.+)" />
    </conditions>
</rule>
<rule name="DoAuthRewrite" stopProcessing="true">
    <match url="(.*)" />
    <action type="Rewrite" url="SOMETHING_ELSE" />
    <conditions>
        <add input="{HTTP_COOKIE}" pattern="ServerProxy=(.+)" negate="true" />
    </conditions>
</rule>

Note that * have been changed to + to make sure that cookie is not empty. Negate simply flips condition, so makes it empty or non-existant.

like image 100
Migol Avatar answered Oct 13 '22 20:10

Migol