title: How can I rewrite an already rewritten file if it is not found
body: <p>I need to rewrite a URL for an image file. This is the easy part and looks like this:</p>
  
  <pre><code>RewriteRule ^img[/]?(/[^.]+)?$ - [L]
  RewriteRule ^img/([A-Za-z0-9_-\s]+)/([A-Za-z0-9_\-\s\.]+)?$ cache/$1-$2 [L]
  </code></pre>
  
  <p>However if the image file is not found in that cache directory I need to rewrite this again to a PHP script that will create the image and save it in that cache directory.</p>
  
  <p>I tried the following rewrite, but it doesn't work as intended. Instead all image URLs are rewritten to the PHP file regardless if the file exists in the cache or not.</p>
  
  <pre><code>#RewriteCond cache/%{REQUEST_FILENAME} !-f
  #RewriteCond cache/%{REQUEST_FILENAME} !-d
  #RewriteRule ^cache/([A-Za-z0-9_\s]+)-([A-Za-z0-9_\-\s\.]+)?$ createimg.php?type=1&amp;hash=$1&amp;filename=$2 [L]
  </code></pre>
  
  <p>Any ideas? Thanks.</p>