web.config redirect non-www to www
Asked Answered
M

8

51

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="^example.com$" />
    </conditions>
    <action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>

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

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

Memling answered 18/7, 2013 at 4:49 Comment(1)
first rule catchs both requests' types and second will not be processedInexplicable
U
68

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

Unswerving answered 18/7, 2013 at 5:57 Comment(8)
It's worth noting that you need URL Rewrite Module 2.0 (http://www.iis.net/downloads/microsoft/url-rewrite) for this to work.Maugham
note to self: REPLACE domain.com with your own domain <slaps head>Phio
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!Sianna
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.Merilee
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."Simferopol
Shouldn't the pattern escape the dot character? <add input="{HTTP_HOST}" pattern="^domain\.com$" />Apodaca
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?Sammy
Is there a way to make the pattern matched more generic? Or is there specific reasoning to manually setting the specific domain?Jetty
F
26

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.

Fionafionna answered 21/9, 2017 at 21:31 Comment(6)
I'm logged in for +1 for youSiderosis
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" />Siderosis
You actually answer the question instead of playing around with certificatesKapor
Upvote for being more generic and usable without having to know the domain name.Jetty
This generic answer should be switched to accepted.Rhythm
Wanted to accept this answer but it did not work for me. It didn't contain the .com after the redirect.Peltier
M
12

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

Mane answered 19/4, 2018 at 9:8 Comment(0)
A
2

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>
Arcanum answered 13/6, 2017 at 11:0 Comment(0)
H
1

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>
Highsounding answered 7/3, 2019 at 10:48 Comment(0)
H
1

Add these rules to your web config file.

Rule 1: HTTP_to_HTTPS_redirect will redirect your website from HTTP to HTTPS and

Rule 2: Redirect to www will redirect your non-www URL to the www URL.

<rewrite>
  <rules>
    <rule name="HTTP_to_HTTPS_redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
    <rule name="Redirect to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^example.com$" />
      </conditions>
      <action type="Redirect" url="http://www.example.com/{R:1}" />
    </rule>
  </rules>
</rewrite>
Hawkweed answered 11/4, 2023 at 10:11 Comment(0)
C
0

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 example.com to www.example.com HTTPS” patternSyntax=”ECMAScript” 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: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…!!

Castellano answered 21/2, 2020 at 6:17 Comment(0)
V
-4

Follow the Step Below

  1. Login to your website's cPanel

  2. Click on Hosting Setting

  3. Select your preferred domain as www.example.com

Visional answered 21/2, 2019 at 16:18 Comment(1)
This is not at all what the question was. The question is about IIS, not whatever specific hosting provider you have.Phung

© 2022 - 2025 — McMap. All rights reserved.