Wednesday, 28 April 2021

IIS HTTPS redirect via web.config

 Open web.config and insert the following

<system.webServer>   
   <rewrite> 
      <rules> 
      <rule name="Force HTTPS" enabled="true">   
   <match url="(.*)" ignoreCase="false"/>   
   <conditions>   
      <add input="{HTTPS}" pattern="off"/>   
   </conditions> 
   <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent"/> 
      </rule> 
      </rules> 
   </rewrite>
</system.webServer>

If you already have <system.webserver> headings,  just insert this code in there and remove the duplicate heading.

If you using Lets encrypt for SSL you will need to do the following instead

         <rewrite>
            <rules>
                <rule name="Allow LetsEncrypt" patternSyntax="Wildcard" stopProcessing="true">
                    <match url=".well-known/*" />
                    <action type="None" />
                </rule>
                <rule name="Redirect HTTP to HTTPS" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*" ignoreCase="false" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
                </rule>
            </rules>
        </rewrite>

No comments:

Post a Comment