0
votes

I have the following situation with url rewrite rules which are getting conflicted with each other:

  1. Rule 1: I need to redirect my domain to https
  2. Rule 2: I need to redirect www.mydomain.com --> https://mydomain.com
  3. Rule 3: I need both www.mydomain.com and mydomain.com to redirect to https://mydomain.com/myfolder, but if I have mydomain.com/mysecondfolder should only redirct to https://mydomain.com/mysecondfolder

what I was able to achieve is everything but redirecting www.mydomain.com to https://mydomain.com (just because it is being conflicted with another rules, if alone it is working).

My rules are:

<rule name="HTTP to HTTPS redirect" stopProcessing="true">
              <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" />
                </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
            </rule> 

            <rule name="redirect to myfolder" enabled="true">
                <match url="^$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Rewrite" url="/myfolder" />
            </rule>
1

1 Answers

0
votes

I was able to solve this using the below rules:

<rewrite>
            <rules>
            <rule name="Canonical Host Name" enabled="true" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTP_HOST}" negate="true" pattern="^myapp\.com$" />
              </conditions>
              <action type="Redirect" url="http://myapp.com/{R:1}" redirectType="Permanent" />
            </rule>
                <rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
                  <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
                </rule> 
                <rule name="redirect to items" enabled="false">
                    <match url="^$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/items" />
                </rule>

            </rules>
        </rewrite>