Windows server web.config file to redirect all URLS into HTTPS

Used below code to redirect all URLs into HTTPS in windows server using web.config file.

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite> 
<rules>
 
 
<rule name="Force HTTPS" enabled="true" stopProcessing="true">>
<match url="(.*)" /> 
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" ignoreCase="true" /> 
<add input="{REQUEST_URI}" pattern="^(.*)/ccadmin(.*)" ignoreCase="true" negate="true" /> 
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
 
 
 
<rule name="Remove index.php Rule" stopProcessing="true"> 
<match url=".*" ignoreCase="false" /> 
<conditions> 
<add input="{URL}" pattern="^/(media|skin|js)/" ignoreCase="false" negate="true" /> 
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" /> 
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" /> 
</conditions> 
<action type="Rewrite" url="index.php" /> 
</rule>
 
 
 
</rules>
</rewrite>
</system.webServer>
<system.net>
<mailSettings>
<smtp>
<network host="localhost" />
</smtp>
</mailSettings>
</system.net>
</configuration>

Only below code worked for HTTPS redirection:

<rule name="Force HTTPS" enabled="true" stopProcessing="true">>
<match url="(.*)" /> 
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" ignoreCase="true" /> 
<add input="{REQUEST_URI}" pattern="^(.*)/admin(.*)" ignoreCase="true" negate="true" />  <!-- Restrict the admin URLS from HTTPS redirection -->
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

 

 

Used below code to restrict particular URL from HTTPS redirection based on URL slug.

From the above code “MatchAll” value allow to work the HTTPS redirection once all the condition success within the <conditions> tag

logicalGrouping="MatchAll"

 

So, I have added two conditions like below within <conditions> tag.

  1. Check the loading URL is HTTPS or Not
  2. Check the loading URL slug
<add input="{HTTPS}" pattern="off" ignoreCase="true" /> 
<add input="{REQUEST_URI}" pattern="^(.*)/admin(.*)" ignoreCase="true" negate="true" />  <!-- Restrict the admin URLS from HTTPS redirection -->

 

So, above code restrict the admin URL from HTTPS redirection.

Leave a Reply

Your email address will not be published. Required fields are marked *