Multiple RewriteConditions:如何在一组规则之前链接它们? [英] Multiple RewriteConditions: How to chain them before a set of rules?

查看:22
本文介绍了Multiple RewriteConditions:如何在一组规则之前链接它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Q1:如何链接这两个条件使它们如果同时存在 A 和 B,则继续...
Q2:如何让它们坚持下面的所有重写规则,而不仅仅是第一条规则?

Q1: How to chain these two conditions making them if BOTH A AND B, then proceed...
Q2: How to make them stick for all the rewriteRules below and not just the first rule?

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 每分钟.但这是配置而不是代码,所以这些规则不适用,对吧?:-)

Here are some tricks for making a RewriteCond stack apply to multiple RewriteRule's, sorted by increasing WTF's per minute. But this is configuration and not code, so those rules don't apply, right? :-)

当您有很多 RewriteCond 时,将它们的结果存储在一个环境变量中,然后在每个规则中进行测试会更加紧凑.

When you have many RewriteCond's, storing their result in an environment variable and then testing against in every rule is more compact.

# 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] 标志,您可以跳过整个 RewriteRule 块.

2. Skip flag

This one's a bit subtle. Using the [S] or [skip] flag you can cause your entire block of RewriteRule's to be skipped.

# 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

这有点像一个 if 语句,RewriteCond 是条件,RewriteRule 是代码块.

This acts sort of like an if-statement with the RewriteCond's being the condition and RewriteRule's being the code block.

你会减少重复,但代价是代码不太清楚,每次你从这组中添加或删除规则时,你必须更新[skip=N]N RewriteRule's.

You get less duplication, but the tradeoff is the code is less clear, and you have to update [skip=N] every time you add or remove a rule from this set of N RewriteRule's.

好吧,如果你还在阅读,这里你会发现另外两个解决方案,其中 WTF's 每分钟达到并超过临界点.它们仅供娱乐,你会明白为什么.

All right, if you're still reading, here you'll find two more solutions where the WTF's per minute reach and exceed a critical point. They're for amusement only, and you'll see why.

是的,有一种方法可以使用 [skip] 标志而不包括 N,您想要的 RewriteRule 的数量应用 RewriteCond 堆栈.也就是说...如果您在每个 RewriteRule 之前包含一对 RewriteCond,哦,是的,最后还有一个.

Yes, there is a way to use the [skip] flag without including N, the number of RewriteRule's you want to apply the RewriteCond stack to. That is... if you include a pair of RewriteCond's before each RewriteRule, and oh yeah, one more at the end.

# 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

这里的技巧是,当且仅当 RewriteCond 成功时,每个 [skip=1] 规则都会被处理,并且每个 [skip=2] 当且仅当它们失败时,规则才会被处理.

The trick here is that every [skip=1] rule gets processed if and only if the RewriteCond's succeeded, and every [skip=2] rule gets processed if and only if they failed.

使用 URL 的一部分来保存状态,然后在您的 RewriteRule 中匹配它.

Use part of the URL to hold state then match against it in your RewriteRule's.

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

The new URL with the marker is invalid, but the last RewriteRule revert it, right? Well, only if it gets processed, so don't let the marker URL escape this round of mod_rewrite processing before it gets reverted. You'll get a 404 then.

这篇关于Multiple RewriteConditions:如何在一组规则之前链接它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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