title: Regex to match any .html file, but only in root dir for IIS Redirect
body: <p>The Question <a href="https://stackoverflow.com/questions/4899419/regular-expression-to-match-resources-only-contained-at-root">here</a> would seem to answer my quesiton, however the answers for it fail pattern testing in IIS as well as being flagged as invalid on regex101.com.</p>
  
  <p>I'm trying to redirect mobile clients to a more mobile friendly version of my site hosted on a subdomain (m.mydomin.com), but I have a bunch of other scripts and folders that are not (yet) gotten their mobile friendly counterparts. Manglement has decreed that I start redirecting clients for the sections of the site that are mobile ready.</p>
  
  <p>Basically what I need is a rewrite rule to fire only when:</p>
  
  <ol>
  <li>Client is a mobile client</li>
  <li>Request is for an *.html file in the ROOT.</li>
  </ol>
  
  <p>So basically:</p>
  
  <ul>
  <li>/index.html</li>
  <li>/somepage.html</li>
  <li>/someotherpage.html</li>
  </ul>
  
  <p>Should be redirected for mobile clients, but:</p>
  
  <ul>
  <li>/somescript.php</li>
  <li>/some/dir/page.html</li>
  <li>/ script.php?omgwtfisgoingon=true</li>
  </ul>
  
  <p>Should not get redirected.</p>
  
  <p>Here is my current redirect that works fine, but fires on all pages for any mobile client:</p>
  
  <pre><code>            &lt;rule name="Mobile Rewrite" patternSyntax="ECMAScript" stopProcessing="true"&gt;
                  &lt;match url=".*" ignoreCase="true" negate="false" /&gt;
                  &lt;conditions logicalGrouping="MatchAny" trackAllCaptures="false"&gt;
                      &lt;add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" /&gt;
                  &lt;/conditions&gt;
                  &lt;action type="Redirect" url="http://m.domain.com/{ToLower:{R:0}}" appendQueryString="true" redirectType="Found" /&gt;
              &lt;/rule&gt;
  </code></pre>
  
  <p>I had planned on updating it to change the rule to MATCHALL with a second input checking for the *.html files, but I cannot wrap my brain around regex to get it to work, and unfortunately since these are html files, there's no doing this at code level, has to be done by IIS.</p>
  
  <p>Edit for those looking for the final IIS Redirect code:</p>
  
  <pre><code>            &lt;rule name="Mobile Rewrite" patternSyntax="ECMAScript" stopProcessing="true"&gt;
                  &lt;match url=".*" ignoreCase="true" negate="false" /&gt;
                  &lt;conditions logicalGrouping="MatchAll" trackAllCaptures="false"&gt;
                      &lt;add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" /&gt;
                      &lt;add input="{REQUEST_URI}" pattern="^\/[^\/]+\.html$" /&gt;
                  &lt;/conditions&gt;
                  &lt;action type="Redirect" url="http://m.domain.com//{ToLower:{R:0}}" appendQueryString="true" redirectType="Found" /&gt;
              &lt;/rule&gt;
  </code></pre>