title: IIS URL Rewrite - with multiple domains
body: <p>I have a website/application where the same pages run many websites (depending on the URL the pages display different logo/css/content etc). I am trying to force each website domain to redirect to the www. verison of the domain.</p>
  
  <p>I have used the default Canonical Rule to start with in URL Rewrite then just changed the Match URL Pattern from:</p>
  
  <pre><code>(.*)
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>(^domain*)
  </code></pre>
  
  <p>So that it will only do this for the non-www. version of the current domain and not match for all domains.</p>
  
  <p>My plan is to create one of these for each domain that uses the system to redirect each one to the www. version.</p>
  
  <p>When I test the pattern in IIS the rule matches as (I think) it should do. But when I go to the website it doesn't redirect, it just stays on the non www. version.</p>
  
  <p>Does anyone know where i'm going wrong or if this is even possible?</p>
  
  <p>Thanks
  J.</p>
  
  <p><strong>UPDATE</strong>
  Thought i'd try and give a better explanation:</p>
  
  <p>I have one website setup in IIS. The pages of this website run many websites of the same type, the style, logo and products on each website change depending on the URL.</p>
  
  <p>So say I have the following bindings setup on the website:</p>
  
  <p>www.domain1.com, domain1.com, www.domain2.com, domain2.com, www.domain3.com, domain3.com</p>
  
  <p>The website will serve up a different looking website, with different content, styles etc depending on the URL.</p>
  
  <p>If I then use the default 'Canonical domain name' rule in URL Rewrite you have to pick one domain to forward to, so if i chose www.domain1.com it adds rule:</p>
  
  <pre><code>&lt;rewrite&gt;
      &lt;rules&gt;
          &lt;rule name="CanonicalHostNameRule1"&gt;
              &lt;match url="(.*)" /&gt;
              &lt;conditions&gt;
                  &lt;add input="{HTTP_HOST}" pattern="^www\.domain1\.com$" negate="true" /&gt;
              &lt;/conditions&gt;
              &lt;action type="Redirect" url="http://www.domain1.com/{R:1}" /&gt;
          &lt;/rule&gt;
        &lt;/rules&gt;
  &lt;/rewrite&gt;
  </code></pre>
  
  <p>This then forwards everything to www.domain1.com. So if a user visits domain2.com or www.domain2.com they will get redirected to www.domain1.com.</p>
  
  <p>I need it so that people get redirected as follows:</p>
  
  <p>domain1.com > www.domain1.com</p>
  
  <p>domain2.com > www.domain2.com</p>
  
  <p>domain3.com > www.domain3.com</p>
  
  <p>When testing new websites to add to the existing ones, we set them up on a sub domain, so I also require sub domains to continue to not be re-directed.</p>
  
  <p>But the issue is with doing this within the same website/web.config file. </p>