How to redirect all non-www URLs to https://www. in IIS?

I want to add the proper 301 permanent redirect rule in IIS 8.5. I’ve added the following rules but it is not working.

 <rule name="Redirect top domains with non-www to www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*stage..*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*dev..*" negate="true" />
            <add input="{HTTP_HOST}" pattern="^(http://){0,1}(www.){0,1}([^.]+).([^.]+)$" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://www.{C:3}.{C:4}" redirectType="Permanent" />
        </rule>

Conditions

To summarize, every URL should be in HTTPS and should have “www.” prefix.

NOTE: I have installed URL Rewrite Module in IIS.

Can anyone please help me to achieve this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I managed it by adding two URL rewrite rules in Web.config file:

  1. For redirecting non-www to https://www.{domain}.com/…
  2. Redirecting HTTP to HTTPS
    <rule name="Redirect top domains with non-www to www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*stage..*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*dev..*" negate="true" />
            <add input="{HTTP_HOST}" pattern="^([^.]+).([^.]+)$" />
          </conditions>
          <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
          <serverVariables>
            <set name="Redirect" value="false" />
          </serverVariables>
     </rule>
    
    
    <rule name="Force HTTPS" enabled="true" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*stage..*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*dev..*" negate="true" />
            <add input="{HTTPS}" pattern="off" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
     </rule>

All the conditions with negate="true" are used for exclusion. Hence all URLs which contains “localhost”, “stage”, and “dev” are excluded from URL rewrite. You can remove these conditions if not required.

Read more about negate attribute at http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x