用URL重写卡在htaccess的使用Apache:1规则来统治他们 [英] Stuck with URL-rewriting in htaccess with Apache: 1 Rule to Rule them all
问题描述
审视自己当前的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终极重写URL,这给了我自由改写下面的网址与任何财产,而不必硬code重写,每个可能性一个规则的,准确的顺序:目前我code是非常愚蠢的,inpractical&安培;它不完全unelegant,思想确实工作!
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] +)
这部分是匹配的,但preserved。这是$ 1的结果。
^(IMG-[a-z0-9]+/[a-z0-9]+)
This part is matched but preserved. This is $1 in the result.
_
A单下划线。这是匹配的,而是被忽略。
_
A single underscore. This is matched but discarded.
([A-Z0-9])
匹配单个字母或数字(显然undorscore以下)。这为$ 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]
(没有更通用的规则将匹配)
(no more generic rules will match)
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...
- 一切(除了下划线,目录斜杠和周期)是一个字母或数字。可能是过于严格的你,如果文件或参数可能包含国际字符。
- 参数名称始终是一个字符长,但不必明确列出。
- 图片永远不会超过一个子文件夹深(容易修复。)
告诉我你的想法。
这篇关于用URL重写卡在htaccess的使用Apache:1规则来统治他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!