gpt4 book ai didi

apache - 多个重写条件 : How to chain them before a set of rules?

转载 作者:行者123 更新时间:2023-12-03 11:03:59 24 4
gpt4 key购买 nike

第一季度 : 如何链接这两个条件使它们if BOTH A AND B, then proceed... Q2 : 如何让他们遵守下面的所有 rewriteRules 而不仅仅是第一条规则?

RewriteCond %{REQUEST_URI} ^IMAGE-.*$      // if filename starts with IMG- and,
RewriteCond %{REQUEST_FILENAME} !-f // if file does exist, then proceed:
RewriteRule Rule1
RewriteRule Rule2
RewriteRule Rule3

# -- END IF -- STOP HERE -- #

最佳答案

以下是制作 RewriteCond 的一些技巧堆栈适用于多个 RewriteRule的,按递增排序 WTF's per minute .但这是配置而不是代码,所以这些规则不适用,对吧? :-)

1.环境变量

当你有很多 RewriteCond 's,将它们的结果存储在环境变量中,然后在每个规则中进行测试更加紧凑。

# Your RewriteCond stack.
RewriteCond %{REQUEST_URI} !^IMAGE-.*$ [OR]
RewriteCond %{REQUEST_FILENAME} -f
# Store environment variable.
RewriteRule ^ - [E=TRUE:YEP]
# Assert environment variable in remaining RewriteRule's.
RewriteCond %{ENV:TRUE} =YEP
RewriteRule Rule1
RewriteCond %{ENV:TRUE} =YEP
RewriteRule Rule2
RewriteCond %{ENV:TRUE} =YEP
RewriteRule Rule3

2.跳过标志

这个有点微妙。使用 [S][skip]标记你可以导致你的整个 block RewriteRule是要跳过的。
# Your RewriteCond stack.
RewriteCond %{REQUEST_URI} !^IMAGE-.*$ [OR]
RewriteCond %{REQUEST_FILENAME} -f
# If RewriteCond's match, skip the next RewriteRule.
RewriteRule ^ - [skip=1]
# Otherwise, this rule will match and the rest will be skipped.
RewriteRule ^ - [skip=3]
RewriteRule Rule1
RewriteRule Rule2
RewriteRule Rule3

这有点像带有 RewriteCond 的 if 语句。是条件和 RewriteRule是代码块。

你得到的重复更少,但代价是代码不太清晰,你必须更新 [skip=N]每次从这组 中添加或删除规则时否 RewriteRule的。

<乐趣>

好吧,如果你还在阅读,在这里你会发现另外两个解决方案,其中 WTF's per minute达到并超过临界点。它们只是为了娱乐,你会明白为什么。

3.跳过没有N的标志

是的,有一种方法可以使用 [skip]不包括 的标志否 , 数量 RewriteRule你想申请 RewriteCond堆栈到。也就是说...如果您包含一对 RewriteCond在每个 RewriteRule 之前,哦,是的,最后还有一个。
# Your RewriteCond stack.
RewriteCond %{REQUEST_URI} !^IMAGE-.*$ [OR]
RewriteCond %{REQUEST_FILENAME} -f
# If RewriteCond's match, skip the next RewriteRule.
RewriteRule ^ - [skip=1] # succeeded
RewriteRule ^ - [skip=2] # failed
RewriteRule Rule1
RewriteRule ^ - [skip=1] # succeeded
RewriteRule ^ - [skip=2] # failed
RewriteRule Rule2
RewriteRule ^ - [skip=1] # succeeded
RewriteRule ^ - [skip=2] # failed
RewriteRule Rule3
RewriteRule ^ - # no-op to cover for last [skip=2] rule

这里的诀窍是每个 [skip=1]当且仅当 RewriteCond的成功,并且每个 [skip=2]当且仅当它们失败时,才会处理规则。

4. 网址标记

使用部分 URL 来保持状态,然后在您的 RewriteRule 中匹配它的。
# Your RewriteCond stack.
RewriteCond %{REQUEST_URI} !^IMAGE-.*$ [OR]
RewriteCond %{REQUEST_FILENAME} -f
# If RewriteCond's match, prepend bogus marker "M#" to internal URL.
RewriteRule .* M#$0
# All your RewriteRule's test for this marker plus whatever else.
RewriteRule ^M#.*Rule1
RewriteRule ^M#.*Rule2
RewriteRule ^M#.*Rule3
# Finally, don't forget to strip off the bogus marker.
RewriteRule ^M#(.*) $1

带有标记的新 URL 无效,但最后一个 RewriteRule恢复它,对吧?好吧,只有当它被处理时,所以不要让标记 URL 在它被还原之前逃脱这一轮 mod_rewrite 处理。然后你会得到一个404。

关于apache - 多个重写条件 : How to chain them before a set of rules?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5305987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com