redirect http to https all pages of website

Sudesh Sharma 176 Reputation points
2021-09-21T12:05:31.023+00:00

Hey Guys

I have created and hosted a react node web app on azure VM. I have purchased a wildcard SSL Certificate from azure. I have defined below web.config file for URL redirection.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>

            <rule name="httptohttps" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://%{HTTP_HOST}%{REQUEST_URI}" />
            </rule>
            <rule name="non-wwwtowww" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^abc\.com$" />
                </conditions>
                <action type="Redirect" url="https://www.abc.com/{R:1}" />
            </rule>
        </rules>  
    </rewrite>
</system.webServer>  

</configuration>

Above URL redirection works for only the main page abc.com not for all other pages of the website.

For example, If the user enters http://abc.com/home then the page redirects to https://www.abc.com/home

How it's possible in web.config file

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
6,981 questions
{count} votes

2 answers

Sort by: Most helpful
  1. KarishmaTiwari-MSFT 17,997 Reputation points Microsoft Employee
    2021-10-01T23:41:18.86+00:00

    Can you please check the following posts which might help resolve this issue :

    1. https://stackoverflow.com/questions/39914184/wildcard-subdomains-on-azure
    2. This explains some ways to create the rule in portal itself by adding a new routing rule rather than the app level

    https://learn.microsoft.com/en-us/azure/frontdoor/front-door-how-to-redirect-https

    1 person found this answer helpful.
    0 comments No comments

  2. MotoX80 31,391 Reputation points
    2021-10-02T14:01:12.887+00:00

    Does your web site use relative links for all images/stylesheets/links/etc?

    What I used to do was to define 2 web sites. The main site only had the HTTPS binding. The second "redirect site" only had the HTTP binding.

    On the "redirect site", configure it to send everything to the HTTPS site. That should avoid having to add a bunch of rewrite rules.

    Set the home directory for the redirect site to an empty folder. Or maybe just put a default page there that says that something went wrong.

    137038-capture.jpg

    1 person found this answer helpful.
    0 comments No comments