Apache-2.2

是否有一個優雅的解決方案來重寫長圖像?query-urls 到 cachable_nice_urls,只使用 apache htaccess?

  • December 15, 2010

這是一個**痛苦的問題!*誰解決了這個謎題,誰就會收到貝寶捐款來吃兩個人的晚餐。現在讓我們開始吧。給定:大多數代理,不要用“?”記憶體資源 即使響應中存在 Cache-control: public 標頭,在他們的 URL 中也是如此。要為這些資源啟用代理記憶體,我必須從對靜態資源的引用中刪除查詢字元串,而是將參數編碼為文件名本身。

以前有

<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" />

工作中

<img src"/IMG-folder/photo_w100_h100_cf_q60.jpg"/>
  • 圖像將位於根目錄深處的 1 個文件夾中(永遠不會更深)
  • img.php?src= 或 img?src= 將永遠一致地命名為
  • ….jpg &w,&h,&q,&m, &f 和 &c 是唯一需要變數的字母(例如:&w=200&h&=50&c=p&f=bw&q=90),但都應該是可選的

以這個不優雅的程式碼為代價

# 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]

但是,仍然發現了無窮無盡的其他可能性:例如,不包括首先給定高度的情況,等等不,這不起作用!如何重定向 OPTIONALLY ALL 參數?&w= 或 &h= 類似 &

$$ a-z $$=(.*) 這樣 _h 或 _w 或 _m 都是通用且可選的,並且當被重寫的代理友好 URL 干預時? 所以現在的任務是:將

後面的任何或所有查詢重寫[photo123.jpg]&[a-z]=(.*)&[a-z]=(.*)etcetteraphoto123_X-x_Y-y(optionally more queries).jpg

我建議逐步更換欄位。類似於以下內容:

(未經測試)

# 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]

引用自:https://serverfault.com/questions/209437