title: RewriteRule checking file in rewriten file path exists
body: <p>How can you use ModRewrite to check if a cache file exists, and if it does, rewrite to the cache file and otherwise rewrite to a dynamic file. </p>
  
  <p>For example I have the following folder structure:</p>
  
  <pre>
  pages.php
  cache/
    pages/
     1.html
     2.html
     textToo.html
     etc.
  </pre>
  
  <p>How would you setup the RewriteRules for this so request can be send like this:</p>
  
  <pre>
  example.com/pages/1
  </pre>
  
  <p>And if the cache file exists rewrite tot the cache file, and if the cache file does not exists, rewrite to pages.php?p=1</p>
  
  <p>It should be something like this: (note that this does not work, otherwise I would not have asked this)</p>
  
  <pre>
  RewriteRule ^pages/([^/\.]+) cache/pages/$1.html [NC,QSA]
  RewriteCond %{REQUEST_FILENAME} -f [NC,OR] 
  RewriteCond %{REQUEST_FILENAME} -d [NC] 
  RewriteRule cache/pages/([^/\.]+).html pages.php?p=$1 [NC,QSA,L]
  </pre>
  
  <p>I can off coarse do this using PHP but I thought it had to be possible using mod_rewrite.</p>