title: redirect subfolder to subdomain/subfolder using IIS URL Rewrite
body: <p>I am looking at using IIS Rewrite Module 2.0 to redirect some broken application links.</p>
  
  <p>Previously www.mydomain.com was our corporate site and www.mydomain.com/application1, www.mydomain.com/application2 were other applications our customers used. They were all on the same server.</p>
  
  <p>Now we are having our corporate site updated and moved to another server. So www.mydomain.com and mydomain.com resolve to a different server. As part of this work, a subdomain of app.mydomain.com was created to house the customer applications still residing on our server.</p>
  
  <p>What I want to be able to do is install the rewrite module on the new corporate site's server and redirect www.mydomain/application1 or mydomain.com/application1 to app.mydomain.com/application1.</p>
  
  <p>I have the list applications so maybe using a rewriteMap is the way to go? That would prevent any redirects like www.mydomain.com/newcorporateurl being wrongly redirected. I also need to preserve subfolders and querystrings (due to people having browser bookmarks) like www.mydomain.com/application1/index.aspx?id=1 needs to go to app.mydomain.com/application1/index.aspx?id=1</p>
  
  <p>Any help putting this together would be great! Even just getting the basic config sorted so I can have a play with it and understand it a bit more. I have been browsing other questions and resources but am still struggling how to put this together</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>The below doesn't work but hopefully it demonstrates what I am trying to do.
  I have about 15 or applications I need to redirect in this way.</p>
  
  <pre><code>&lt;rewrite&gt;
      &lt;rules&gt;
          &lt;rule name="application1 rule" stopProcessing="true"&gt;
              &lt;match url="(www\.)?mydomain\.com(:80)?/application1/?" /&gt;
              &lt;action type="Redirect" url="app.mydomain.com/application1/" appendQueryString="false" /&gt;
          &lt;/rule&gt;
          &lt;rule name="application2 rule" stopProcessing="true"&gt;
              &lt;match url="(www\.)?mydomain\.com(:80)?/application2/?" /&gt;
              &lt;action type="Redirect" url="app.mydomain.com/application2/" appendQueryString="false" /&gt;
          &lt;/rule&gt;
      &lt;/rules&gt;
  &lt;/rewrite&gt;
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>I have this working to an extent</p>
  
  <pre><code>&lt;rewrite&gt;
      &lt;rules&gt;
          &lt;clear /&gt;
          &lt;rule name="application1 rule" stopProcessing="true"&gt;
              &lt;match url="application1(.*)" /&gt;
              &lt;conditions&gt;
                  &lt;add input="{HTTP_HOST}" pattern="mydomain.com" /&gt;
              &lt;/conditions&gt;
              &lt;action type="Redirect" url="http://app.mydomain.com/application1{R:1}" /&gt;
          &lt;/rule&gt;
      &lt;/rules&gt;
  &lt;/rewrite&gt;
  </code></pre>
  
  <p>This will redirect</p>
  
  <p>www.mydomain.com/application1 -> app.mydomain.com/application1
  www.mydomain.com/application1/index.aspx -> app.mydomain.com/application1/index.aspx</p>
  
  <p>The issue I want to resolve now is this.</p>
  
  <p>The new site at www.mydomain.com is a CMS. With the above rule, it means the CMS can't contain 'application1' at all in the URI.</p>
  
  <p>Real world example is 'commercial'</p>
  
  <p>Can someone please help refining my rule so the following is true</p>
  
  <p>www.mydomain.com/commercial -> app.mydomain.com/commercial
  www.mydomain.com/commercial/foo/index.aspx -> app.mydomain.com/foo/index.aspx</p>
  
  <p>www.mydomain.com/tv-commercials/list -> www.mydomain.com/tv-commercials/list
  www.mydomain.com/commercial-property/list -> www.mydomain.com/commercial-property/list</p>