3

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

Sam
  • 423
  • 3
  • 7
  • 23

2 Answers2

1

I would suggest progressively replacing fields. Something along the lines of:

(untested)

# Rewrite imgcpu?src= thumbnail maker to nice static urls
RewriteCond %{REQUEST_URI} ^IMG.*$
RewriteRule ^IMG-(.+)_w([^_]+)(_.*)$ imgcpu\?src=$1\.jpg&w=$2$3 [C]
RewriteRule ^(imgcpu[^_]*)_h([^_]+)(_.*)$ $1&h=$2$3 [C]
RewriteRule ^(imgcpu[^_]*)_q([^_]+)(_.*)$ $1&q=$2$3

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:

# Rewrite imgcpu?src= thumbnail maker to nice static urls
RewriteCond %{REQUEST_URI} ^IMG.*$
RewriteRule ^IMG-(.+\/photo_[^_]+)(_.*)*(\.[^.]+)$ imgcpu\?src=$1$3$2/ [C]
RewriteRule ^(imgcpu.*\..+)_w([^_]+)(_.*)*$ $1&w=$2$3 [C]
RewriteRule ^(imgcpu.*\..+)_h([^_]+)(_.*)*$ $1&h=$2$3 [C]
RewriteRule ^(imgcpu.*\..+)_q([^_]+)(_.*)*$ $1&q=$2$3 [C]
RewriteRule ^(imgcpu.*\..+)_c([^_]+)(_.*)*$ $1&c=$2$3 [C]
RewriteRule ^(imgcpu.*\..+)_m([^_]+)(_.*)*$ $1&m=$2$3 [C]
RewriteRule ^(imgcpu.*\..+)_f([^_]+)(_.*)*$ $1&f=$2$3 [C]

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).

RewriteRule ^IMG-(.+\/.*?)(_[hw]\d+)([^.]*)(\.[^.]+)$/imgcpu\?src=$1$4$2$3/ [C]

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):

RewriteCond %{QUERY_STRING} ^(.*\.[^_]*)_(.)([^_]*)(_.*)?$
RewriteRule ^/imgcpu /imgcpu?%1&%2=%3%4 [N]
RewriteCond %{REQUEST_URI} ^/IMG.*$
RewriteRule ^/IMG-(.+/.*?)(_[hw]\d+)([^.]*)(\.[^.]+)$ /imgcpu?src=$1$4$2$3 [N]
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • 1
    Sam might also want to add a `RewriteCond %{REQUEST_URI} ^IMG.*$` before that block so Apache isn't running every single request through all those RewriteRules needlessly. – Chris S Dec 06 '10 at 14:33
  • Thanks Chris and Dennis! Chris, will add your smart line above all. Im farily new to these scripts, couldyou please help and provide one more line for example for **&q=** sothat I can see how to further fill in? – Sam Dec 06 '10 at 16:12
  • 1
    @Sam: See my edited answer. – Dennis Williamson Dec 06 '10 at 16:18
  • 1
    @Sam: I'm sorry, I left out a couple of parentheses. See my edited answer. – Dennis Williamson Dec 08 '10 at 18:45
  • Dear Dennis, thanks somuch for your support, but, it still doesnt work! have pasted the rules (deleted my own long rules) then pasted yours and edited (see below my question) but images dont appear then. With the long rules they do fine. Someting is wrong but donno what... – Sam Dec 10 '10 at 02:26
  • 1
    @Sam: I have edited my answer with a feeble attempt at correcting my previous wrongs. You will see me claim to have been half-asleep and make other excuses and claim that *this time for sure it will work*. Don't believe me *for a second!* Take a look at the new information. This time it **will** work - *I just know it*. **zzz...zzz** – Dennis Williamson Dec 10 '10 at 03:38
  • @Sam: See I told you. Hold on ... more corrections on the way. – Dennis Williamson Dec 10 '10 at 03:43
  • 1
    @Sam: Weeeellll... no promises, but give it a try. – Dennis Williamson Dec 10 '10 at 05:07
  • wauw i will give it a try looks beautifully elegant (there are no wrong answers in art, only in mathematics... pff) – Sam Dec 13 '10 at 17:16
  • Hi Dennis, Looks priomising, we are almost at it , it now sais FILE not found , i know why! my example files "photo_frogs.jpg" were just example, that means, none of my photos actually starts with [photo_]. My photos start with normal characters followed by some underscores untill the name of the photo is done, then the optional one character querys _w _h _f etc etc finally [.jpg]! typical file name is something like [christmas_warmth_drawing_01_w200_h100_q75.jpg] what needs to be changed in the apachee then? – Sam Dec 14 '10 at 03:06
  • by the way a cool website / blog u have! i hope too they land on Mars in a shortwhile. at least in our lifetime we can experience it. (ps over here many folks say there is proof that no man has actually set foot on mars! bizzarr) – Sam Dec 14 '10 at 03:08
  • Brainwave: so, the distintive separation between filename and query is formed by either the _w[number] or _h[number] or both _w200_h200 !! see also note i left just a minute a go on Marks reply. – Sam Dec 14 '10 at 03:39
  • @Sam: I've added something for you to try. – Dennis Williamson Dec 14 '10 at 04:41
  • sight, dear Dennis im almost feeling there is just no way to reduce that ugly code of the question any neater... after numerous tries each and every error i get with your otherwise NEAT code is:`[Wed Dec 15 00:47:54 2010] [error] File does not exist: /httpdocs/IMG-folder_images` is there any way i can test your code in a step by step method? Is it better to start a new topic since this topic is already answered: I started with urls with ? & string queries and now have statis urls. That part is done. second part is to beautifyl the code. So I think its better to migrate the bonus 2nd part! – Sam Dec 14 '10 at 23:51
  • here is the follow up Dennis: http://serverfault.com/questions/212681/url-rewrite-to-make-generated-thumnails-cache-like-static-a-syntax-question let me thank you by wishing to provide you a donation for your very involved comments (will need a paypal email) and, invite you to join the second part and post your solution théré freshly!! – Sam Dec 15 '10 at 00:14
1

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:

RewriteRule ^(.*?\IMG-)(.*)_(\?[^_]*)?_([^_]*)_([^_]*)(.*)$ /imgcpu?src=(?2$2&:\?)$4=$5$6 [LP]

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.

Mark Henderson
  • 68,823
  • 31
  • 180
  • 259
  • Thanks Mark for your completely new insight. Before i test it a quick question: what are besides ` - ` other valid normal-problem-free delimiters that proxy caches love? I CAN use other delimiters, BUT verymuch prefer the shorter `-w350-h10-q90.jpg` as oposed to `-w-350-h-10-q-90.jpg` – Sam Dec 07 '10 at 05:53
  • @Sam: I think Mark meant only in the filename. So it would look like `` – Dennis Williamson Dec 10 '10 at 03:42
  • 1
    @Dennis - thanks, yes you're correct. @Sam - sorry I didn't see your comment there. The reason you want to avoid using _ in the filename is so that its easy to tell what's the filename and what's the delimiter. – Mark Henderson Dec 10 '10 at 03:50
  • true, but what all file names do have in common, is that they have a width or height or both set no filenames will have _w[number] or _h[number] or both as part of the filename !! does this give me any credits for being consistent? – Sam Dec 14 '10 at 03:40
  • @Sam - the advantage of using a different delimiter is that if you have them all the same, then you need to search for `_w` OR `_h` OR `_c` etc. etc. , and if you need to add a new type (say, `_a`) then you need to adjust all your rewriting rules to match. If you saved `_` as a delimiter ONLY then you just need search for `_` and you **know** that the next character is the parameter, and everything from then to the next `_` is the value. – Mark Henderson Dec 14 '10 at 03:46