问题描述
检查我当前用于加载图片的 url 重写:
examine my current url rewrites for loading pictures:
// Possibiities: 1 letter char, followed by `=` then the value (numbers/letters)
// w= | h= | c= | q= | f=
IMG-bla/blue_w100.jpg >> imgcpu?src=bla/blue.jpg&w=100
IMG-bla/blue_cp.jpg >> imgcpu?src=bla/blue.jpg&c=p
IMG-bla/blue_h200.jpg >> imgcpu?src=bla/blue.jpg&h=200
IMG-bla/blue_w50_h200_fbw.jpg >> imgcpu?src=bla/blue.jpg&w=50&h=200&f=bw
本质上,我希望有 1 个 Ultimate 重写 url,这使我可以自由地使用任何属性重写以下 url,而无需对重写的确切顺序进行硬编码,每种可能性都有一个规则:目前我的代码非常愚蠢, 不切实际的 &不完全它的不雅,思想确实有效!
Essentially I would like to have 1 Ultimate rewrite url, that gives me the freedom to rewrite the below urls with any property, without having to hardcode that exact sequence of rewriting, with one rule per possibility: currently my code is very stupid, inpractical & incomplete its unelegant, thought does work!
// works but utterly unelegant and unpractical as well as incomplete:
RewriteRule ^IMG-(.+)_w(.+).jpg$ imgcpu\.php\?src=$1\.jpg&w=$2 [L]
RewriteRule ^IMG-(.+)_w(.+)_h(.+)_c(.+).jpg$ imgcpu\.php\?src=$1\.jpg&w=$2&h=$3&c=$4 [L]
RewriteRule ^IMG-(.+)_w(.+)_h(.+)_f(.+).jpg$ imgcpu\.php\?src=$1\.jpg&w=$2&h=$3&f=$4 [L]
现在,我怎样才能用可选的财产资产重写这个复杂的规则?我正在寻找 1 条规则来统治它们.你的想法 &欢迎提出建议,我对任何敢于解决这个复杂难题的人都赞不绝口!
Now, how can I rewrite this complex rule with the optional property assets? I am in search for that 1 Rule to Rule them all. Your ideas & suggestions are warmly welcome and I deliciously upvote anything that dares to solve this complex puzzle!
推荐答案
在得出这个非常通用的解决方案之前,我考虑了几种方法.在这里,我多次重复相同的规则.这是一种分而治之的策略,每次执行规则时,它都会提取一个参数.
I pondered several methods before reaching this very general solution. Here, I have repeated the same rule several times. It's a sort of divide and conquer strategy where each time the rule is executed, it extracts one parameter.
# Generic rule for extracting one parameter
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
# Final rule which does the actual rewrite
RewriteRule IMG-([a-z0-9]+/[a-z0-9]+\.jpg) /test/index.php?src=$1 [QSA]
让我们将 D&C 规则分解成更小的部分!
Let's break the D&C rule into smaller parts!
^(IMG-[a-z0-9]+/[a-z0-9]+)
这部分匹配但保留.结果是 1 美元.
^(IMG-[a-z0-9]+/[a-z0-9]+)
This part is matched but preserved. This is $1 in the result.
_
单个下划线.这是匹配但被丢弃的.
_
A single underscore. This is matched but discarded.
([a-z0-9])
匹配单个字母或数字(显然跟在反划线之后).这是 $2,假定为参数名称.
([a-z0-9])
matches a single letter or number (obviously following the undorscore). This is $2 and is assumed to be the parameter name.
([a-z0-9]+)
匹配一个或多个字母或数字.这是结果中的 $3,并假定为参数值.
([a-z0-9]+)
matches one or more letters or numbers. This is $3 in the result and is assumed to be the parameter value.
([_.].*)$
匹配单个下划线或句点,以及直到请求字符串末尾的所有其他内容.这是 4 美元.
([_.].*)$
matches a single underscore or period, and everything else up to the end of the request string. This is $4.
因此,对于每一步,都会提取一个参数(如果匹配),然后在 QSA 的帮助下将其附加到查询字符串中.其余的请求,1 美元 4 美元捆绑在一起,准备好进行另一轮.冲洗并重复,直到我们超出参数或达到可用规则的最大数量.
So for every step, one parameter is extracted (if there was a match, that is) which is then appended to the query string with the help of QSA. The rest of the request, $1$4 is bundled together and made ready for another round. Rinse and repeat until we're out of parameters or reach the maximum number of rules available.
最终规则匹配到那时应该是路径和文件名的内容.这些是步骤:
The final rule matches what should be a path and filename by then.These are the steps:
IMG-bla/blue_w50_h200_fbw.jpg -> IMG-bla/blue_h200_fbw.jpg?w=50 [with QSA]
IMG-bla/blue_h200_fbw.jpg -> IMG-bla/blue_fbw.jpg?h=200 [with QSA]
IMG-bla/blue_fbw.jpg -> IMG-bla/blue.jpg?f=bw [with QSA]
(不再匹配通用规则)
IMG-bla/blue.jpg -> /test/index.php?src=bla/blue.jpg [with QSA]
附加查询字符串后的总计:
Grand total after the query strings are appended:
/test/index.php?src=bla/blue.jpg&f=bw&h=200&w=50
请注意,我是在做一些假设...
Note that I'm making a few assumptions...
- 一切(除了下划线、目录斜线和句点)都是一个字母或数字.如果文件或参数可能包含国际字符,可能对您来说太严格了.
- 参数名称总是一个字符长,但不需要明确列出.
- 图像的深度永远不会超过一个子文件夹(易于修复.)
告诉我你的想法.
这篇关于使用 Apache 在 htaccess 中进行 URL 重写:1 条规则来统治所有这些的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!