htaccess 中的变量,RewriteRule 问题

发布于 2024-09-08 07:48:58 字数 630 浏览 0 评论 0原文

我如何使用 RewriteRule 访问变量?

如果我有:

SetEnv MY_VAR (a|b|c)

RewriteRule ^%{ENV:MY_VAR}$ index.php?s=$1 [L,QSA]

RewriteRule ^%{ENV:MY_VAR}-some-one$ index.php?s=$1 [L,QSA]

我有这些例子,但不起作用。

稍后编辑

好的蒂姆,谢谢你的回答。假设我有:(

RewriteRule ^(skoda|bmw|mercedes)-([0-9]+)-([0-9]+)-some-think$ index.php?a=$1 [L,QSA]

RewriteRule ^some-one-(skoda|bmw|mercedes)/pag-([0-9]+)$ index.php?a=$1 [L,QSA]

RewriteRule ^a-z-(skoda|bmw|mercedes)$ index.php?a=$1 [L,QSA]

忘记 RewriteRule 的第二部分).. 我不想把这个列表放在任何地方(skoda|bmw|mercedes)。创建变量比在规则中使用它更快......

How could i use a RewriteRule accesing a variable?

If i have:

SetEnv MY_VAR (a|b|c)

RewriteRule ^%{ENV:MY_VAR}$ index.php?s=$1 [L,QSA]

RewriteRule ^%{ENV:MY_VAR}-some-one$ index.php?s=$1 [L,QSA]

I have these examples but doesn`t work.

Later edit

Ok Tim, thank you for the answer. Let say that i have:

RewriteRule ^(skoda|bmw|mercedes)-([0-9]+)-([0-9]+)-some-think$ index.php?a=$1 [L,QSA]

RewriteRule ^some-one-(skoda|bmw|mercedes)/pag-([0-9]+)$ index.php?a=$1 [L,QSA]

RewriteRule ^a-z-(skoda|bmw|mercedes)$ index.php?a=$1 [L,QSA]

(forget second part of RewriteRule) .. I don-t want to put everywhere (skoda|bmw|mercedes) this list. Is more quickly to make a variable then to use it in rule...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

季末如歌 2024-09-15 07:48:58

您不能这样做,因为 mod_rewrite 不会扩展正则表达式子句中的变量。

您只能在 RewriteCond 的输入参数中使用变量,并作为 RewriteRule 的结果参数。编译正则表达式会产生开销(特别是如果您被迫像 .htaccess 文件一样针对每个请求执行此操作),因此如果您允许其中包含可变内容,则必须重新编译它们每次比较都以牺牲性能为代价来确保准确性。因此,解决方案似乎就是不让你这样做。

你到底想这么做是为了什么?

You can't do that, because mod_rewrite doesn't expand variables in the regular expression clauses.

You can only use variables in the input argument to a RewriteCond, and as the result argument to a RewriteRule. There's overhead in compiling the regular expressions (especially if you're forced to do it per-request as with .htaccess files), so if you allowed variable content in them, they'd have to be recompiled for every comparison to ensure accuracy at the cost of performance. It seems the solution therefore was to not let you do that.

What exactly did you want to do that for, anyway?

自由如风 2024-09-15 07:48:58

我在 jdMorgan 的 mod_rewrite 论坛 上收到了另一个答案:

Mod_rewrite 无法使用正则表达式模式中的变量。 .htaccess 指令不是脚本语言...

我建议:

RewriteCond $1<>$3 ^<>-([0-9]+)-([0-9]+)-some-think$ [OR]
RewriteCond $1<>$3 ^some-one-<>/pag-([0-9]+)$ [OR]
RewriteCond $1<>$3 ^a-z+-<>$
RewriteRule ^([^\-/]+[\-/])*(skoda|bmw|mercedes)([\-/].+)?$ index.php?a=$2 [QSA,L]

在这里,首先评估 RewriteRule 模式(请参阅 Apache mod_rewrite 文档“规则处理”)。

如果模式匹配,则请求的 URL 路径中“(skoda|bmw|mercedes)”之前的任何内容都会被放入局部变量“$1”中。

“(skoda|bmw|mercedes)”后面的任何内容都被放入局部变量 $3 中。

所请求的 URL 路径匹配“(skoda|bmw|mercedes)”的值被放入 $2 中。

然后处理每个 RewriteConds 以检查不带“(skoda|bmw|mercedes)”部分的请求 URL 的格式是否是要接受的格式之一。

注意“<>”字符仅用作分隔符以帮助正确且明确的解析,并且在此使用时没有特殊含义。它们只是“取代”您不想包含在每行中的变量字符串。您可以使用您确信在不首先进行 URL 编码的情况下绝不会出现在 URL 中的任何字符。我更喜欢使用 > 中的任何一个或<或者〜我自己。

另请注意,RewriteRule 假定“(skoda|bmw|mercedes)”子字符串始终由连字符或斜杠分隔(如果其前面或后面有任何其他子字符串)。我指的是包含“[^-/]”(“不是连字符或斜杠”)和“[-/]”(“匹配连字符或斜杠”)的两个 RewriteRule 子模式。这极大地提高了正则表达式模式匹配的效率,因此如果可能的话请使用此方法,而不是使用像“.*”这样不明确且低效的子模式(匹配任何内容、所有内容或什么都不匹配)。

I`ve received another answer on a mod_rewrite forum from jdMorgan:

Mod_rewrite cannot use a variable in a regex pattern. The .htaccess directives are not a scripting language...

I'd recommend:

RewriteCond $1<>$3 ^<>-([0-9]+)-([0-9]+)-some-think$ [OR]
RewriteCond $1<>$3 ^some-one-<>/pag-([0-9]+)$ [OR]
RewriteCond $1<>$3 ^a-z+-<>$
RewriteRule ^([^\-/]+[\-/])*(skoda|bmw|mercedes)([\-/].+)?$ index.php?a=$2 [QSA,L]

Here, the RewriteRule pattern is evaluated first (See Apache mod_rewrite documentation "Rule Processing").

If the pattern matches, then whatever comes before "(skoda|bmw|mercedes)" in the requested URL-path is placed into local variable "$1".

Whatever follows "(skoda|bmw|mercedes)" is placed into local variable $3.

The value of the requested URL-path matching "(skoda|bmw|mercedes)" is placed into $2.

Then each of the RewriteConds is processed to check that the format of the requested URL without the "(skoda|bmw|mercedes)" part is one of the formats to be accepted.

Note that the "<>" characters are used only as a separator to assist correct and unambiguous parsing, and have no special meaning as used here. They simply "take the place of" the variable-string that you do not want to include in each line. You can use any character or characters that you are sure will never appear in one of your URLs without first being URL-encoded. I prefer to use any of > or < or ~ myself.

Note also that the RewriteRule assumes that the "(skoda|bmw|mercedes)" substring will always be delimited by either a hyphen or a slash if any other substring precedes or follows it. I am referring to the two RewriteRule sub-patterns containing "[^-/]" ("NOT a hyphen or a slash") and "[-/]" ("Match a hyphen or a slash"). This greatly improves efficiency of regular-expressions pattern matching, so use this method if possible instead of using an ambiguous and inefficient sub-pattern like ".*" (Match anything, everything, or nothing").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文