Redirect from http to https and add www

aywsdown 21 Reputation points
2021-04-29T16:29:29.92+00:00

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?

Internet Information Services
0 comments No comments
{count} votes

Accepted answer
  1. Sam Wu-MSFT 7,531 Reputation points Microsoft Vendor
    2021-04-30T02:40:31.083+00:00

    Hi @aywsdown

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.