有没有一个优雅的解决scheme,重写长图像?查询url到cachable_nice_urls,只使用Apache htaccess?

这是一个屁股问题痛苦! 天才解决这个谜语将获得贝宝捐赠吃两个晚餐。 现在让我们开始吧。 鉴于:大多数代理,不要caching资源与“?” 即使在响应中存在Cache-control:public头部,它们的URL也是如此。 要为这些资源启用代理caching,我必须从对静态资源的引用中删除查询string,而不是将参数编码到文件名称中。

以前有过

<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是调用variables的唯一字母(例如:&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] 

然而,还有其他的可能性还没有被发现:例如,不包括首先给出高度的情况等等。不,这不行! 我怎样才能redirectOPTIONALLY所有参数? &w =或&h =类似于&[az] =(。*)之类的东西,_h或_w或_m都是通用的和可选的,并且在被改写的,代理友好的URL中进行插入时?

所以现在的任务是:
[photo123.jpg]&[az]=(.*)&[az]=(.*)etcettera任何或所有查询[photo123.jpg]&[az]=(.*)&[az]=(.*)etcetteraphoto123_X-x_Y-y(optionally more queries).jpg

我会build议逐步取代领域。 有些东西是:

(另)

 # 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 

直到你把所有的领域逐个转换,而不是一次性完成所有的领域。 每个规则将被连续应用。 这应该适用于任何顺序的字段,并允许它们是可选的。

[C]离开这条规则链的最后一个。

编辑:

这比我到目前为止发表的任何废话都更有可能:

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

主要尝试的改进包括星号在第一个美元符号之前,如果被重写的字段是查询string中的最后一个,并且将第一个[^_]*更改为。 请注意,我是如何留下最后一句话 – 希望不会被注意到? 等等…什么? 注意它试图被忽视? 说到不注意…我只是注意到,文件名包括下划线!

编辑: Fer真正的时间 – 嘘shur!

编辑:

试试这个代替第一个RewriteRule (所有其他的行保持不变)。

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

这取决于字段中的第一个高度或宽度字段。 你可以在[hw]添加更多的字母。 但是,请注意,像“/IMG-folder/santa_claus_w2_elves_w200_h100_q75.jpg”(圣诞老人瓦特/ 2精灵)这样的文件名不会给出正确的结果。

编辑(12月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] 

我有一个正则expression式做了一次非常相似,但有一个不同的格式。 我已经把它弄糊涂了一些,但是这完全没有经过testing和未完成(更不用说我frackin'HATE正则expression式的),但希望它会让你的方式:

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

基本上,它会拉出你的URL的第一部分,然后继续循环,直到没有更多的_字符,以格式&$1=$2附加到URL。

这将意味着你将不得不使用/IMG-folder1/photo_citty_w_3500_h_10_c_p_q_90.jpg的格式,但我相信你可以解决这个问题,只是简单的一个字符,而不是使用下划线。

我可以build议,如果你的分隔符( _ )没有用在URL的文件名部分,那么你会让你的生活变得如此简单? 还有很多其他的分隔符可以用来减less重写规则的复杂度。