1

In a Azure portal app, I configured traffic to redirect to https but https://www won't redirect to https://

Redirection from http://, http://www both work correctly.

Those are rules I have in web.config in azure app.

<system.webServer>
<rewrite>
  <rules>
   <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>
  <rule name="NonWwwRedirect"  stopProcessing="true"> 
      <match url="(.*)" /> 
      <conditions> 
          <add input="{HTTP_HOST}" pattern="^www\.example\.com$" /> 
      </conditions> 
      <action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" /> 
  </rule> 
  <!--To always remove trailing slash from the URL-->
    <rule name="Remove trailing slash" stopProcessing="true">
      <match url="(.*)/$" />
      <action type="Redirect" redirectType="Permanent" url="{R:1}" />
    </rule>
    <rule name="AngularJS 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" />
        <add input="{REQUEST_URI}" pattern="^/(signalr)" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(token)" negate="true" />
      </conditions>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

How can I achieve the needed redirect?

Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
Dazhush
  • 1,575
  • 1
  • 12
  • 18

3 Answers3

1

The problem was with DNS records. A record with host www was pointed to different IP. Changed it to same IP as @ host, that solved the problem

Dazhush
  • 1,575
  • 1
  • 12
  • 18
0

How about:

<rewrite>
  <rules>
    <rule name="Redirect www OR non-https to https://" enabled="true" stopProcessing="true">
      <match url=".*" ignoreCase="true" />
      <conditions logicalGrouping="MatchAny>
        <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
Leo
  • 5,013
  • 1
  • 28
  • 65
-1

You could refer to the code as below:

<rule name="NonWwwRedirect" stopProcessing="true">
    <match url=".*"/>
    <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
    </conditions>
    <action type="Redirect" url="https://example.com/{R:0}" redirectType="Permanent" />
</rule> 

Note: Make sure the example.com hostname you have assigned to Site, so that you could reach it successfully.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30