title: Apache RewriteCond: how to match only top-level requests (no subdirectory)
body: <p>After banging my head against this for the better part of a week, it turned out to be the same problem, and solution, as in this thread: <a href="https://stackoverflow.com/questions/4824964/rewritecond-in-htaccess-with-negated-regex-condition-doesnt-work">RewriteCond in .htaccess with negated regex condition doesn&#39;t work?</a></p>
  
  <p>TL;DR: I had deleted my 404 document at some point. This was causing Apache to run through the rules again when it tried to serve the new page and couldn't. On the second trip through, it would always match my special conditions.</p>
  
  <hr>
  
  <p>I'm having endless trouble with this regex, and I don't know whether it's because I'm missing something about RewriteCond or what.</p>
  
  <p>Simply, I want to match only top-level requests, meaning any request with no subdirectory. For example I want to match <code>site.com/index.html</code>, but not <code>site.com/subdirectory/index.html</code>.</p>
  
  <p>I thought I would be able to accomplish it with this:</p>
  
  <pre><code>RewriteCond %{REQUEST_URI} !/[^/]+/.*
  </code></pre>
  
  <p>The interesting thing is, it doesn't work <em>but the reverse does</em>. For example:</p>
  
  <pre><code>RewriteCond %{REQUEST_URI} /[^/]+/.*
  </code></pre>
  
  <p>That will detect when there <em>is</em> a subdirectory. And it will omit top-level requests (<code>site.com/toplevelurl</code>). But when I put the exclamation point in front to reverse the rule (which RewriteCond is supposed to allow), it stops matching anything.</p>
  
  <p>I've tried many different flavors of regex and different patterns that should work, but none seem to. Any help would be appreciated. <a href="https://stackoverflow.com/questions/12115580/regex-match-any-subdirectory">this Stack Overflow</a> answer seems like it should answer it but does not work for me.</p>
  
  <p>I've also tested it with this <a href="http://htaccess.madewithlove.be/" rel="nofollow noreferrer">.htaccess rule tester</a>, and my patterns work in the tester, they just don't work on the actual server.</p>
  
  <hr>
  
  <p>Edit: by request, here is my .htaccess. It allows URLs without file extensions and also does something similar to a custom 404 page (although its purpose is to allow filenames as arguments, not be a 404 replacement).</p>
  
  <pre><code>Options +FollowSymLinks
  
  DirectoryIndex index.php index.html index.htm
  
  &lt;IfModule mod_rewrite.c&gt;
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f 
  RewriteCond %{REQUEST_FILENAME}\.html -f 
  RewriteRule ^(.*)$ $1.html
  RewriteCond %{REQUEST_FILENAME} !-f 
  RewriteCond %{REQUEST_FILENAME}\.php -f 
  RewriteRule ^(.*)$ $1.php
  RewriteCond %{REQUEST_FILENAME} =/home/me/public_html/site/
  RewriteRule ^(.*)$ index.php
  RewriteCond %{REQUEST_FILENAME} !-f # Below this is where I would like the new rule
  RewriteRule ^(.*)$ newurl.php
  &lt;/IfModule&gt;
  </code></pre>