URL rewrite web.config with location https

1.3k views Asked by At

I want to redirect my extranet site to https I have the following web.config rule:

 <location path="." inheritInChildApplications="false">
  <system.webServer>
    <rewrite>
      <rules>

      <rule name="Redirect to https" stopProcessing="true">
       <match url="(.*)" />
        <conditions>
         <add input="{HTTPS}" pattern="off" `enter code here`ignoreCase="true" />
        </conditions>
       <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" 
                  redirectType="Permanent" appendQueryString="false" />
      </rule>
     </rules>
   </rewrite>
  </system.webServer>
</location>

The problem comes because I have several services hosted on my server that are children, and I don't want them to redirect too, I just want to redirect the site not the services.

1

There are 1 answers

0
dana On

One way to do this is to by default redirect to HTTPS except for a specified list of folders. In the below example, folders /service1/ and /service2/ are excluded from redirection. Everything else goes to HTTPS.

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Redirect to https" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    <!-- avoid redirection for the following paths -->
                    <add input="{PATH_INFO}" pattern="^/service1/" ignoreCase="true" negate="true" />
                    <add input="{PATH_INFO}" pattern="^/service2/" ignoreCase="true" negate="true" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>