web.config redirect non-www to www

77,778

Solution 1

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="^example.com$" />
      </conditions>
      <action type="Redirect"
        url="{MapProtocol:{HTTPS}}://www.example.com/{R:1}" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="MapProtocol">
      <add key="on" value="https" />
      <add key="off" value="http" />
    </rewriteMap>
  </rewriteMaps>
</rewrite>

Reference

Solution 2

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.

Solution 3

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

Solution 4

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>

Solution 5

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>
Share:
77,778
Anand
Author by

Anand

I am a web developer.

Updated on July 09, 2022

Comments

  • Anand
    Anand almost 2 years

    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.

  • Mattias Larsson
    Mattias Larsson over 9 years
    It's worth noting that you need URL Rewrite Module 2.0 (http://www.iis.net/downloads/microsoft/url-rewrite) for this to work.
  • wal
    wal about 9 years
    note to self: REPLACE domain.com with your own domain <slaps head>
  • Joseph Casey
    Joseph Casey about 9 years
    Additional Note: Make sure you set your [Bindings...] to include the http and https for both www.domain.com and domain.com. You can't use redirect rules if you aren't binding the domain!
  • test
    test almost 9 years
    How to redirect example.com/subsite from non www to www? I tried above one but it only works for home page not inner pages.
  • Letoncse
    Letoncse almost 9 years
    When i added this this code in my web.config file i am getting compile error " Unable to start debugging on the web server. Make sure the server is operating correctly. Verify there are no syntax errors in web.config by doing a Debug.Start Without Debugging. You may also want to refer to the ASP.NET and ATL Server debugging topic in the online documentation."
  • CB_Ron
    CB_Ron about 7 years
    Shouldn't the pattern escape the dot character? <add input="{HTTP_HOST}" pattern="^domain\.com$" />
  • HOÀNG LONG
    HOÀNG LONG over 5 years
    I'm logged in for +1 for you
  • HOÀNG LONG
    HOÀNG LONG over 5 years
    abc.com ->www.abc.com true abc.com/a.aspx->www.com/a.aspx false. Please help me. Tried: <action type="Redirect" url="{C:1}://www.{C:2}/{R:1}" redirectType="Permanent" />
  • ClosDesign
    ClosDesign over 5 years
    So that said, If you use the ^domain.com, will this redirect all subdomains to the www domain as well, or will it skip the subdomains?
  • Phil C
    Phil C over 4 years
    You actually answer the question instead of playing around with certificates
  • Paesano2000
    Paesano2000 over 4 years
    Is there a way to make the pattern matched more generic? Or is there specific reasoning to manually setting the specific domain?
  • Paesano2000
    Paesano2000 over 4 years
    Upvote for being more generic and usable without having to know the domain name.
  • Johannes Mols
    Johannes Mols about 3 years
    This is not at all what the question was. The question is about IIS, not whatever specific hosting provider you have.
  • iGanja
    iGanja about 2 years
    This generic answer should be switched to accepted.