2
votes

I have an Azure web application and need help with enforcing https.

https://www.example.com to https://example.com

http://www.example.com to https://example.com

www.example.com to https://example.com

example.com to https://example.com

The current web.config is as below. This does not work for https://www.example.com -> https://example.com

<rules>


        <!-- Force HTTPS for all requests -->
        <rule name="HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
          <match url="*" />
          <conditions>
            <add input="{HTTPS}" pattern="off" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
        </rule>


        <!-- Redirect all requests to non www-->
        <rule name="WWW" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
          </conditions>
          <action type="Redirect" url="https://example.com{PATH_INFO}" />
        </rule>

</rules>
2

2 Answers

4
votes

According to your url rewrite rules, I assumed that there is no problem with your rules. Though I do not configure the custom domain, I just test it on my azure web app which just contains some static html files and the web.config file, then I force it redirect from bruce-webapp.azurewebsites.net to https://azurewebsites.net as follows:

<rule name="Remove www" stopProcessing="true">
   <match url="^(.*)$" />
   <conditions>
      <add input="{HTTP_HOST}" pattern="^(bruce-webapp\.)(.*)$" />
   </conditions>
   <action type="Redirect" url="https://azurewebsites.net{PATH_INFO}"/>
</rule>

Also, please use the following rule for removing the www prefix to narrow this issue:

<rule name="Remove WWW prefix" stopProcessing="true">
    <match url="(.*)" ignoreCase="true" />
    <conditions>
       <add input="{HTTP_HOST}" pattern="^www\.yourdomain\.com$" />
    </conditions>
    <action type="Redirect" url="https://yourdomain.com/{R:1}" redirectType="Permanent" />
</rule>

If the above trials still could not solve your issue, you could leverage fiddler to collect the network traces to troubleshoot this issue. Also, you could update your question with more details (fiddler network traces or what is the occurrence when you browser it via the browser) for us to locate this issue.

1
votes

You can do this with two separate rules. Set the below configuration in your web.config file.

<rewrite>
    <rules>
        <rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="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>

    <rewrite>
    <rules>
        <rule name="Redirects to www.domain.com" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^domain.com$" />
            </conditions>
            <action type="Redirect" url="https://example.com/{R:0}" />
        </rule>
    </rules>

</rewrite>

Hope this helps.