Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple URL rewrite rules in a web.config

Tags:

How to add multiple URL rewrite rules in a web.config

I want to match any url that contains "sales" and "registrationsuccess".

I get errors with when I try the following:

  <rewrite>
    <rules>
        <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
            <match url="(.*sales*)"/>
           <match url="(.*registrationsuccess*)"/>
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$"/>
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
        </rule>
    </rules>
</rewrite>
like image 885
Milligran Avatar asked Mar 20 '14 00:03

Milligran


People also ask

Where do I put rules in Web config?

All rules must be added in the <rewrite> element inside <system. webServer> . There are two types of rules available: inbound rules (defined in the <rules> element) and outbound rules (defined in the <outboundRules> element). In the example above, the rewrite rule is added as an inbound rule.


1 Answers

This is how you add more than one rule:

  <rewrite>
    <rules>
        <rule name="Redirect HTTP to HTTPS (Sales)" stopProcessing="true">
            <match url="(.*sales*)"/>
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$"/>
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
        </rule>
        <rule name="Redirect HTTP to HTTPS (RegistrationSucces)" stopProcessing="true">
            <match url="(.*registrationsuccess*)"/>
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$"/>
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
        </rule>
    </rules>
</rewrite>

But for what you're trying to do, you can do it with one rule, like below:

<rule name="Redirect to HTTPS" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="ON" />
        <add input="{PATH_INFO}" pattern="^(.*)/sales/(.*)" />
        <add input="{PATH_INFO}" pattern="^(.*)/registrationsuccess/(.*)" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
like image 114
Tom Hall Avatar answered Oct 06 '22 13:10

Tom Hall