46
votes

I need to redirect non-www urls to www url for both http and https urls. I tried following rules in web.config.

<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
    <add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" redirectType="Permanent" />

<rule name="Redirect to WWW https" stopProcessing="true">
<match url=".*" />
<conditions>
    <add input="{HTTPS}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="https://www.domain.com/{R:0}" redirectType="Permanent" />

It works perfectly for non-ssl url but in case of ssl it redirect from https://domain.com to http://www.domain.com

Please help me to correct my rules.

7
first rule catchs both requests' types and second will not be processedDima

7 Answers

61
votes

For a safer rule that works for both Match Any and Match All situations, you can use the Rewrite Map solution. It’s a perfectly good solution with the only drawback being the ever so slight extra effort to set it up since you need to create a rewrite map before you create the rule. In other words, if you choose to use this as your sole method of handling the protocol, you’ll be safe.

You can create a Rewrite Map called MapProtocol, you can use {MapProtocol:{HTTPS}} for the protocol within any rule action.

<rewrite>
  <rules>
    <rule name="Redirect to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^domain.com$" />
      </conditions>
      <action type="Redirect" 
        url="{MapProtocol:{HTTPS}}://www.domain.com/{R:1}" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="MapProtocol">
      <add key="on" value="https" />
      <add key="off" value="http" />
    </rewriteMap>
  </rewriteMaps>
</rewrite>

Reference

22
votes

Other answers here are unnecessary long, here's the rule i use (just copy and paste) :

<rule name="ensurewww" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
  </conditions>
  <action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>

This will redirect to the www version while preserving the HTTP and HTTPS protocol

Hope this helps.

10
votes

Since 2018, consider to use everytime https (SSL) !

So I use redirect to www and to https together.

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

<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{SERVER_PORT_SECURE}" pattern="^0$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>

more info here : https://forums.realmacsoftware.com/t/effective-july-2018-google-s-chrome-browser-will-mark-non-https-sites-as-not-secure/18597

2
votes

I know this is an old post but it's not fully answered. I ended up extending Satpals answer

First rule catches http + www and second one catches non-www

For some reason i could not use MapProtocol in the fist Rule but it works with https written directly into the url.

<rewrite>
  <rules>
    <rule name="Special case www &amp; HTTPS off" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="SeeOther" />
    </rule>
    <rule name="Redirect to www &amp; HTTPS" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^example\.com$" />
      </conditions>
      <action type="Redirect" url="{MapProtocol:{HTTPS}}://www.example.com/{R:1}" redirectType="SeeOther" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="MapProtocol">
      <add key="on" value="https" />
      <add key="off" value="http" />
    </rewriteMap>
  </rewriteMaps>
</rewrite>
1
votes

This worked fine for me:-

<rewrite>
  <rules>
   <rule name="Redirect to WWW" stopProcessing="true">
	<match url=".*" />
	<conditions>
	<add input="{HTTP_HOST}" pattern="^myExample.in$" />
	</conditions>
	<action type="Redirect" url="https://www.myExample.in/{R:0}" redirectType="Permanent" />
</rule>
 </rules>
</rewrite>
0
votes

This post is basically for those who need to redirect non-www to www URL in windows hosting.

In web.config file as below code:

<system.webServer>
<rewrite>
<rules>
<rule name=”Redirect example.com to www.example.com HTTP” patternSyntax=”ECMAScript” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^example.com$” />
<add input=”{HTTPS}” pattern=”off” />
</conditions>
<action type=”Redirect” url=”http://www.example.com/{R:0}” redirectType=”Permanent” appendQueryString=”true”/>
</rule>
<rule name=”Redirect domain.com to www.example.com HTTPS” patternSyntax=”ECMAScript” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^domain.com$” />
<add input=”{HTTPS}” pattern=”on” />
</conditions>
<action type=”Redirect” url=”https://www.example.com/{R:0}” redirectType=”Permanent” appendQueryString=”true”/>
</rule>
</rules>
</rewrite>
</system.webServer>

In the above code please note there are two rules used one for http and another for https. To avoid clashing of http and https in the same rule, here used two separate patterns.

I hope it works for you guys…!!

-4
votes

Follow the Step Below

1) Login to Cpanel of Your Website

2)Click on Hosting Setting

click here to See how to do

3)Select Preferred domain as www.yourdomainame.com eg :www.emodafinil.com

click here to See how to do