简体   繁体   中英

URL rewrite web.config with location https

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM