2

I have problem with my redirection rule. I want my pages will have the "www" prefix. It works on some pages, while on other it simply not doing anything. This is my rule:

    <rule name="WWW Rewrite" enabled="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true"
          pattern="^www\.([.a-zA-Z0-9]+)$" />
      </conditions>
      <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}"
        appendQueryString="true" redirectType="Permanent" />
    </rule>

Any help will be appreciated!

stealthyninja
  • 10,343
  • 11
  • 51
  • 59

2 Answers2

0

You could try this ^(www.)?([.a-zA-Z0-9]+)$

that would match if the page doesnt currently have a "www." and should match if it does or doesnt.

you would need to edit the action to this to pick up the second group <action type="Redirect" url="http://www.{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />

Chadwick13
  • 357
  • 1
  • 7
  • 15
  • nope its not working so good.i think that i didnt give all the inputs. my site is https or http. so when user try to navigate to https://site.com it shuold go to https://wwww.site.com and if user is trying to navigate to http://site.com it should goto http://www.site.com ... **site.com is only a sample. – user1853029 Dec 02 '12 at 12:38
  • How do you decied which ones you want to go to site.com and which go to www.site.com ? – Chadwick13 Dec 04 '12 at 08:53
0

Here is your rule, you just gotta check if your host doesn't match expected host and then redirect to the correct url.

  <rules>
    <rule name="Canonical Host Name" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^www\.domain\.com$" />
      </conditions>
      <action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
Sergey
  • 3,214
  • 5
  • 34
  • 47