title: RewriteRule for image file requests only if a query string is present
body: <p>With my limited regular expression and mod_rewrite abilities, I'm attempting to rewrite certain image requests so I can alter the output with a php script. Here's what I have:</p>
  
  <pre><code>RewriteRule ^(public|uploads)/([A-Za-z0-9/_-]+).(JPEG|JPG|GIF|PNG|jpeg|jpg|gif|png)$ public/images.php?%{QUERY_STRING}&amp;src=$1/$2.$3 [L]
  #            [       1      ] [       2       ] [                  3              ]
  </code></pre>
  
  <p>This does work, but it's too greedy and doesn't <em>require</em> the query string, which is important - otherwise all images requests would be rewritten. I tried putting a <code>?</code> or <code>?(.*)</code> in the rule, and I would either get an internal server error or it didn't seem to solve the problem (most likely because I didn't do it correctly). I also tried <code>%{QUERY_STRING}</code> at the end of the condition, but that didn't seem to affect anything.</p>
  
  <p>Here's what I want to happen:</p>
  
  <ul>
  <li>Any requests for <code>public/</code> or <code>uploads/</code>...</li>
  <li>Followed by any path to an image (file extension case insensitive)...</li>
  <li>Followed by a query string...</li>
  <li>...should rewrite to <code>public/images.php</code> with the original query string, and add one aditional parameter: <code>src</code>, which contains the actual path to the image (the rewritten part).</li>
  <li>Extra "would-be-nice", but not necessary: Restrict the rule to only rewrite the url if the query string contains at least one item from a set of parameters. For example, only if one of the <code>width=</code>, <code>height=</code> or <code>contrast=</code> params are present. If this makes things bloated or complicated, I'm not worried about it.</li>
  </ul>
  
  <p>So for example, a request for:</p>
  
  <pre><code>uploads/images/my_folder/test.jpg?width=320&amp;height=220
  </code></pre>
  
  <p>Should be served by:</p>
  
  <pre><code>public/images.php?width=320&amp;height=220&amp;src=public/images/my_folder/test.jpg
  </code></pre>
  
  <p>The <code>.htaccess</code> file is in my root directory, as well as the <code>public</code> and <code>uploads</code> directories.</p>
  
  <p>I want to avoid absolute urls, because I want this to be portable without needing to edit. I've done a good deal of googling and reading related SO posts, and still can't figure this one out. How can I patch this rule to do what I want, or is there a better way to do write this altogether?</p>
  
  <p><strong>Edit</strong>: Just want to note that this rule worked for me previously:</p>
  
  <pre><code>RewriteRule ^(public|uploads)/([A-Za-z0-9/_-]+).(JPEG|JPG|GIF|PNG|jpeg|jpg|gif|png)/([0-9]+)$ public/images.php?width=$4&amp;src=$1/$2.$3
  </code></pre>
  
  <p>...but only for requests like <code>uploads/my_folder/image.jpg/280</code> - I used the 280 as the width, but now I want to accept combinations of multiple parameters in no particular order:</p>