Is there an Elegant solution to rewrite long image?query-urls into cachable_nice_urls, using only apache htaccess?
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]
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.