this is a pain in the ass-problem! Whicever genius solves this riddle will receive paypal donation to eat diner for two. Now lets get started. Given: most proxies, do not cache resources with a "?" in their URL even if a Cache-control: public header is present in the response. To enable proxy caching for these resources, i have to remove query strings from references to static resources, and instead encode the parameters into the file names themselves.*
PREVIOUSLY HAD
<img src="/imgcpu?src=folder1/photo_citty.jpg&w=3500&h=10&c=p&q=90" />
<img src="/imgcpu?src=folder2/photo_doggy.jpg&w=100&h=200&c=p" />
<img src="/imgcpu?src=folder3/photo_birds.jpg&w=200&h=500&f=bw" />
<img src="/imgcpu?src=folder3/photo_frogs.jpg&h=200" />
NOW WORKING
<img src"/IMG-folder/photo_w100_h100_cf_q60.jpg"/>
- The images will reside in 1 folder deep on the root (never deeper)
- the img.php?src= or img?src= will be consistently named this forever
- after the ....jpg &w,&h,&q,&m, &f and &c are the only letters that call for variables (e.g.: &w=200&h&=50&c=p&f=bw&q=90) but all should be optional
AT THE COST OF THIS UN-ELEGANT CODE
# Rewrite imgcpu?src= thumbnail maker to nice static urls
RewriteCond %{REQUEST_URI} ^IMG.*$
RewriteRule ^IMG-(.+)_w(.+)_h(.+)_c(.+)_q(.+).jpg$ imgcpu\?src=$1\.jpg&w=$2&h=$3&c=$4&q=$5 [L]
RewriteRule ^IMG-(.+)_w(.+)_h(.+)_c(.+)_f(.+)_q(.+).jpg$ imgcpu\?src=$1\.jpg&w=$2&h=$3&c=$4&f=$5&q=$6 [L]
RewriteRule ^IMG-(.+)_w(.+)_h(.+)_c(.+)_f(.+).jpg$ imgcpu\?src=$1\.jpg&w=$2&h=$3&c=$4&f=$5 [L]
RewriteRule ^IMG-(.+)_w(.+)_h(.+)_c(.+)_q(.+).jpg$ imgcpu\?src=$1\.jpg&w=$2&h=$3&c=$4&q=$5 [L]
RewriteRule ^IMG-(.+)_w(.+)_h(.+)_c(.+).jpg$ imgcpu\?src=$1\.jpg&w=$2&h=$3&c=$4 [L]
RewriteRule ^IMG-(.+)_w(.+)_h(.+)_f(.+).jpg$ imgcpu\?src=$1\.jpg&w=$2&h=$3&f=$4 [L]
RewriteRule ^IMG-(.+)_w(.+)_h(.+).jpg$ imgcpu\?src=$1\.jpg&w=$2&h=$3 [L]
RewriteRule ^IMG-(.+)_w(.+).jpg$ imgcpu\?src=$1\.jpg&w=$2 [L]
RewriteRule ^IMG-(.+)_h(.+).jpg$ imgcpu\?src=$1\.jpg&h=$3 [L]
However, endless other possibilities are still uncovered: e.g. doesnt cover the cases where first the height is given the width etc No this shall not work! How can I redirect OPTIONALLY ALL parameter? &w= or &h= with something like &[a-z]=(.*) sothat _h or _w or _m are all universal and optional and when given intervepted by the rewritten, proxy friendly urls?
So the assignment Now is:
Rewrite any or all of the queries that follow [photo123.jpg]&[a-z]=(.*)&[a-z]=(.*)etcettera
into photo123_X-x_Y-y(optionally more queries).jpg
I would suggest progressively replacing fields. Something along the lines of:
(untested)
and so on until you get all the fields converted one by one instead of trying to do them all at once. Each rule will be applied successively. This should work for fields in any order and allow for them to be optional.
Leave the
[C]
off the last of this Chain of rules.Edit:
This is more likely to work than any of the nonsense I've posted so far:
The main attempted improvements involve the asterisk before the first dollar sign which are intended to make things work if the field being rewritten is the last one in the query string and changing the first
[^_]*
to.*
. Notice how I left mentioning the biggest screw-up 'til last - hoping it wouldn't be noticed? Wait ... what? Notice it attempting to be unnoticed? Speaking of not noticing ... I only just noticed that the filenames include underscores!Edit: Fer realz dis time - fer shur!
Edit:
Try this in place of the first
RewriteRule
(all the other lines stay the same).This depends on either the height or width field being first among the fields. You could add more letters in
[hw]
. Note, however, that a filename like the contrived "/IMG-folder/santa_claus_w2_elves_w200_h100_q75.jpg" (Santa Claus w/ 2 elves) would not give correct results.Edit (Dec 15):
I had a regex that did something very similar once, but with a bit of a different format. I've mucked around with it a bit, but this is totally untested and unfinished (not to mention I frackin' HATE regex's), but hopefully it will set you on your way:
Basically, it will pull out your first part of the URL and then keep looping until there are no more
_
characters, appending them in the format of&$1=$2
onto the URL.This will mean you'll have to use the format of
/IMG-folder1/photo_citty_w_3500_h_10_c_p_q_90.jpg
but I'm sure you could fix that to simply step one character rather than use the underscore.May I suggest that you'll make you life so much easier if your delimiter (
_
) is not used in the filename part of the URL? There are plenty of other delimiters you could use (such as a-
) that would reduce the complexity of your rewrite rules.