坚持Ingenius可选URL重写htaccess的使用Apache:1规则来统治他们 [英] Stuck with Ingenius Optional URL-rewriting in htaccess with Apache: 1 Rule to Rule them all

查看:114
本文介绍了坚持Ingenius可选URL重写htaccess的使用Apache:1规则来统治他们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,我检查当前的URL重写加载图片:

  // Possibiities:1个字母字符,其次是'=',则该值(数字/字母)
// 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 =体重
 

基本上我想有1终极重写URL,这给了我自由改写下面的网址与任何财产,而不必硬code,准确的改写,每个可能性一个规则的顺序:目前我code是非常愚蠢的,inpractical和放大器;不完整的unelegant,思想确实工作!

  //作品,但完全unelegant和不切实际的,以及不完整:
重写规则^ IMG  -  _W¯¯JPG $ imgcpu \ .PHP的\ src = $ 1 \ JPG格式&AMP(+)(+);?W = $ 2 [L]
重写规则^ IMG  -  _ W _ ^ h _çJPG $ imgcpu \ .PHP的\ src = $ 1 \ JPG格式和放大器; W = $ 2及H = $ 3及C(+)(+)(+)(+)。? = $ 4 L]
重写规则^ IMG  -  _ W _ ^ h _˚FJPG $ imgcpu \ .PHP的\ src = $ 1 \ JPG格式&AMP(+)(+)(+)(+);?W = $ 2及H = $ 3及˚F = $ 4 L]
 

现在,我怎么可以重写这个复杂的规则与可选的物业资产?我在搜索该1规则来统治他们。你的想法和放大器;建议是热烈欢迎,我美味upvote任何敢于来解决这个复杂的难题!

更新:

由于硝基这是现在在发展,但不工作

 重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0 -9] +)([_]。*)$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则IMG  - ?([A-Z0-9] + / [A-Z0-9] + \ JPG)imgcpu \ .PHP的\ src = $ 1 [QSA]
 

解决方案

我沉吟了几种方法达到这种非常通用的解决方案之前。在这里,我已经重复了同样的规则几次。这是一种分而治之的策略,将在执行规则的每一次,它提取一个参数。

 提取一个参数#通用规则

重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]
重写规则^(IMG- [A-Z0-9] + / [A-Z0-9] +)_([A-Z0-9])([A-Z0-9] +)([_]。* )$ $ 1 $ 4〜$ 2 = $ 3 [NC,QSA]

#最终规则来完成实际的重写
重写规则IMG  - ([A-Z0-9] + / [A-Z0-9] + \ JPG)/test/index.php?src=$1 [QSA]
 

让我们打破了D&放大器;!C规定,成较小的部分。

^(IMG- [A-Z0-9] + / [A-Z0-9] +)这部分匹配的,但preserved。这是$ 1的结果。

_ 单下划线。这是匹配的,而是被忽略。

([A-Z0-9])匹配单个字母或数字(以下明显的undorscore)。这是$ 2和被假定为参数名

([A-Z0-9] +)匹配一个或多个字母或数字。这是在结果$ 3和假设为参数值。

([_]。*)$ 匹配一个下划线或时期,和其他一切多达请求字符串的结尾。这是$ 4

因此​​,对于每一个步骤,一个参数提取(如果有一场比赛,那是),然后追加到QSA的帮助下查询字符串。请求的其余部分,$ 1 $ 4捆绑在了一起,并准备好了又一轮。冲洗和重复,直到我们出的参数或达到规定的最大数量的可用。

最终规则匹配什么应该是由当时的路径和文件名。 这些步骤如下:

  IMG-BLA / blue_w50_h200_fbw.jpg  - > IMG-BLA / blue_h200_fbw.jpg?W = 50与QSA]
IMG-BLA / blue_h200_fbw.jpg  - > IMG-BLA / blue_fbw.jpg?H = 200与QSA]
IMG-BLA / blue_fbw.jpg  - > IMG-BLA / blue.jpg?F =体重[与QSA]
 

(没有更通用的规则将匹配)

  IMG-BLA / blue.jpg  - > /test/index.php?src=bla/blue.jpg [与QSA]
 

的查询字符串被追加后总计:

  /test/index.php?src=bla/blue.jpg&f=bw&h=200&w=50
 

请注意,我正在做一些假设...

  • 在一切(除了下划线,目录斜杠和周期)是一个字母或数字。可能是过于严格的你,如果文件或参数可能包含国际字符。
  • 参数名称始终是一个字符长,但不需要被明确列出。
  • 图片永远不会超过一个子文件夹深(容易修复。)

告诉我你的想法。

Dear folks, 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 

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]

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!

Update:

Thanks to Nitro this is now in development, but does not work.

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]
RewriteRule IMG-([a-z0-9]+/[a-z0-9]+\.jpg) imgcpu\.php\?src=$1 [QSA]

解决方案

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]

Let's break the D&C rule into smaller parts!

^(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]) matches a single letter or number (obviously following the undorscore). This is $2 and is assumed to be the parameter name.

([a-z0-9]+) matches one or more letters or numbers. This is $3 in the result and is assumed to be the parameter value.

([_.].*)$ matches a single underscore or period, and everything else up to the end of the request string. This is $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...

  • Everything (apart from underscores, directory slashes and periods) is a letter or number. Might be too strict for you if files or parameters may contain international chars..
  • Parameter names are always one character long, but don't need to be explicitly listed.
  • Images will never be more than one subfolder deep (easy to fix.)

Tell me what you think.

这篇关于坚持Ingenius可选URL重写htaccess的使用Apache:1规则来统治他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆