question

aywsdown-7048 avatar image
0 Votes"
aywsdown-7048 asked SamWu-MSFT answered

Redirect from http to https and add www

I want to redirect from http to https://www, I tried the following rule, but unfortunately failed.

 <rule name="http to https and www" stopProcessing="true">
  <match url="(.*)" />
    <conditions>
      <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
  <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Temporary" />
 </rule>

Can someone tell me where is the problem?

windows-server-iis
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

SamWu-MSFT avatar image
0 Votes"
SamWu-MSFT answered

Hi @aywsdown-7048

You should use two rules to achieve this. one for http to https and another one for https non-www to https www.

 <rule name="http to https" stopProcessing="true">
   <match url="(.*)" />
     <conditions>
           <add input="{HTTPS}" pattern="^OFF$" />
           <add input="{HTTP_HOST}" pattern="^(www\.)?example.com" />
     </conditions>
   <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Temporary" />
 </rule>

 <rule name="non-www to www" stopProcessing="true">
   <match url="(.*)" />
     <conditions>
         <add input="{HTTP_HOST}" pattern="example.com" />
         <add input="{HTTPS}" pattern="^on$" />
      </conditions>
    <action type="Redirect" url="https://www.example.com/{R:1}" />
  </rule>


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.