Site icon port135.com

How to redirect HTTP requests to HTTPS by using IIS URL Rewrite

One way to ensure that your visitors access to your website via secure connection is to redirect HTTP requests to HTTPS by using IIS URL Rewrite module. You will find step-by-step instructions to configure IIS in this post.

Note that you may need to spend some extra effort on configuration if you have a load balancer that hosts SSL certificates (SSL offloading). Please check the last section (“If your web server is behind a Load Balancer…”) at the end of this post for more information.

Looking for an alternative way to redirect HTTP requests to HTTPS? Check this post out.

Redirect HTTP requests to HTTPS by using IIS URL Rewrite

Follow the steps below to redirect all HTTP requests to your HTTPS URL.

IIS site binding for HTTP requests
Create a new URL Rewrite rule
Match URL and condition for URL Rewrite rule
Redirection type and URL

Web.config changes

When you add, edit or remove a URL Rewrite URL, corresponding web.config file is automatically updated. Go to application folder and open the web.config file if you want to view the changes.

<rewrite>
   <rules>
      <rule name="Redirect Subfolder" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
         <match url="*" ignoreCase="true" />
         <conditions logicalGrouping="MatchAny">
            <add input="{HTTPS}" pattern="off" />
         </conditions>
         <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
      </rule>
   </rules>
</rewrite>

Not working? Try these

If the URL Rewrite rule for redirection is not working (HTTP requests continue to go to HTTP site), I would recommend using Failed Request Tracing (FRT) to troubleshoot redirection. Here is a link for instructions to use FRT for URL Rewrite troubleshooting.

Additionally, try the recommendations below to solve the issue:

Uncheck “Require SSL”

If your web server is behind a Load Balancer…

Many companies leverage load balancers to distribute workload among several web servers. If this is the case in your environment, you may need to change the server variable you use in the condition of your URL Rewrite rule. 

In the instructions above, we used {HTTPS} header to check the usage of HTTPS protocol. The load balancer in your network may remove this header from requests. A better practice in this scenario is to use {HTTP_X_FORWARDED_PROTO} header. Click here for more information.

Here is the web.config entry for an example URL Rewrite rule using {HTTP_X_FORWARDED_PROTO} header:

<rewrite>
  <rules>
    <rule name="HTTPS Redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
  </rules>
</rewrite>

References

Exit mobile version