With my limited regular expression and mod_rewrite capabilities, I am trying to rewrite certain image demands in order to affect the output having a php script. This is what I've:

RewriteRule ^(public|uploads)/([A-Za-z0-9/_-]+).(JPEG|JPG|GIF|PNG|jpeg|jpg|gif|png)$ public/images.php?%{QUERY_STRING}&src=$1/$2.$3 [L]
#            [       1      ] [       2       ] [                  3              ]

This may work, but it is too greedy and does not require the query string, that is important - otherwise all images demands could be rewritten. I attempted placing a ? or ?(.*) within the rule, and that i would either have an internal server error or it did not appear to resolve the issue (probably because I did not get it done properly). I additionally attempted %{QUERY_STRING} in the finish from the condition, but that did not appear to affect anything.

This is what I wish to happen:

  • Any demands for public/ or uploads/...
  • Then any road to a picture (file extension situation insensitive)...
  • Then a question string...
  • ...should rewrite to public/images.php using the original query string, and add one aditional parameter: src, which consists of the particular road to the look (the rewritten part).
  • Extra "would-be-nice", although not necessary: Restrict the rule to simply rewrite the url when the query string consists of a minumum of one item from some parameters. For instance, only when among the width=, height= or contrast= params can be found. If the makes things bloated or complicated, I am not concerned about it.

So for instance, a request:

uploads/images/my_folder/test.jpg?width=320&height=220

Ought to be offered by:

public/images.php?width=320&height=220&src=public/images/my_folder/test.jpg

The .htaccess file is within my root directory, along with the public and uploads sites.

I wish to avoid absolute web addresses, because I would like this to become portable without requiring to edit. I have done a large amount of searching and reading through related SO posts, but still can't figure that one out. How do i patch this rule to complete things i want, or it is possible to better method of doing write this altogether?

Edit: Would like to observe that this rule labored for me personally formerly:

RewriteRule ^(public|uploads)/([A-Za-z0-9/_-]+).(JPEG|JPG|GIF|PNG|jpeg|jpg|gif|png)/([0-9]+)$ public/images.php?width=$4&src=$1/$2.$3

...only for demands like uploads/my_folder/image.jpg/280 - I made use of the 280 because the width, however I wish to accept mixtures of multiple parameters in no particular order:

Two approaches:

1. Give a condition to simply rewrite when query string isn't empty (could be anything):

RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(public|uploads)/([A-Za-z0-9/_-]+)\.(jpe?g|gif|png)$ public/images.php?src=$1/$2.$3 [NC,QSA,L]

2. Give a conditions to simply rewrite when query string isn't empty and it has a minumum of one of individuals parameters present (however the value could be empty):

RewriteCond %{QUERY_STRING} (^|&)width=([^&]*)(&|$) [OR]
RewriteCond %{QUERY_STRING} (^|&)height=([^&]*)(&|$) [OR]
RewriteCond %{QUERY_STRING} (^|&)contrast=([^&]*)(&|$)
RewriteRule ^(public|uploads)/([A-Za-z0-9/_-]+)\.(jpe?g|gif|png)$ public/images.php?src=$1/$2.$3 [NC,QSA,L]

I haven't really examined #-. but should work fine (replicated from fully working code).


NOTES:

You are able to replace ^(public|uploads)/([A-Za-z0-9/_-]+)\.(jpe?g|gif|png)$ by ^((?:public|uploads)/(?:[A-Za-z0-9/_-]+)\.(?:jpe?g|gif|png))$ .. after which rather than src=$1/$2.$3 use just src=$1

Alternatively -- replace ^(public|uploads)/([A-Za-z0-9/_-]+)\.(jpe?g|gif|png)$ by ^(?:public|uploads)/[A-Za-z0-9/_-]+\.(?:jpe?g|gif|png)$ after which use src=${REQUEST_URI} -- the only real difference that src= parameter will begin with leading slash.