多个.htaccess文件中的多个mod_rewrite规则的优先顺序是什么? [英] What is the precedence of multiple mod_rewrite rules in multiple .htaccess files?

查看:113
本文介绍了多个.htaccess文件中的多个mod_rewrite规则的优先顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解遵循重写规则的[L]表示它是最后一个",但我不清楚范围是什么

I understand that the [L] following a rewrite rule means that it's the "last" but I'm not clear what the scope is

例如,当有多个.htaccess文件时,有些文件带有[L],哪些文件将适用?

For example, when there are multiple .htaccess files, some with [L], which ones will apply?

示例:

root.com/subdirectory1/subdirectory2

  ^           ^            ^      
  |           |            |
  A           B            C

如果每个目录中都有一个.htaccess文件...

If there is an .htaccess file in each directory...

  1. 按什么顺序应用?
  2. 如果彼此矛盾,哪个优先?
  3. 它们是否连续申请?第一次的结果传递给下一个吗?
  4. 是否会在较早(或相同)的文件中匹配从后一个规则得到的结果?
  5. 如果以前的文件中有[L],是否会考虑其他文件?
  1. In what order will they apply?
  2. If they contradict each other which takes precedence?
  3. Do they apply in serial? Are the results of the first passed to the next?
  4. Will matches in an earlier (or the same) file that RESULT from a later rule get applied?
  5. If there is an [L] in an earlier file will other files get considered?

推荐答案

[L]标志的确表示"last",但仅适用于当前范围内的规则.从您的问题来看,它仅适用于htaccess文件中的规则.如果子目录中有规则,则父目录中的任何规则都将抛出该窗口.除非您使用RewriteOptions Inherit指令(它将来自父htaccess文件的所有规则附加到规则的末尾),否则根本不会应用它们.

The [L] flag does indeed mean "last", but it only applies to the rules in the current scope. From your question, it only applies to the rules in the htaccess file. If there are rules in a subdirectory, any rules in the parent directory are thrown out the window. They are not applied at all, unless you use the RewriteOptions Inherit directive (which will append to the end of the rules any rules from the parent htaccess file).

给出您的示例:

root.com/subdirectory1/subdirectory2
  ^           ^            ^      
  |           |            |
  A           B            C

如果A,B和C中都有htaccess文件,并且全部重写了3条规则,则如果一个请求http://root.com(您的"root.com"目录),则仅应用A中的规则.如果有人请求http://root.com/subdirectory1,则仅应用B中的规则,而忽略A中的任何规则(不带Inherit选项).同样,如果有人进入http://root.com/subdirectory1/subdirectory2,并且在没有继承选项的情况下,则仅应用C中的规则.

If there are htaccess files in A, B, and C, and rewrite rules in all 3, if one requests http://root.com (your "root.com" directory), only the rules in A get applied. If someone requests http://root.com/subdirectory1, then only the rules in B get applied, and any rules in A are ignored (without the Inherit option). Likewise, if someone goes to http://root.com/subdirectory1/subdirectory2, then only the rules in C get applied, if there are no inheriting options.

[L]标志没有任何作用,因为此处的作用域仅在htaccess文件的规则之内.另请注意,[L]不一定表示停止重写 HERE ",因为重写引擎将循环运行,直到进入引擎的URI停止更改为止. [L]只是意味着在重写引擎循环的当前迭代中停止重写.

The [L] flag has no play in any of this, as the scope here is only within the rules of an htaccess file. Also note that [L] doesn't necessarily mean "stop rewriting HERE", as the rewrite engine will loop until the URI going into the engine stops changing. The [L] just means to stop rewriting in the current iteration of the rewrite engine's looping.

在URL处理管道中,apache尝试将URL映射到文件或资源.许多不同的模块都可以在处理管道中发挥作用,例如mod_rewrite或mod_proxy或mod_alias.在任何时候,此URI都可以更改,被标记为重定向,被标记为代理,被标记为引发错误等.当URI到达mod_rewrite时,重写引擎会从vhost配置中收集一堆规则,并且适当的htaccess文件;注意,这里有2个不同的作用域.每个规则范围都应用于URI,如果没有一个规则匹配,则完成mod_rewrite.如果其中一个规则匹配,则存在一个内部重定向,这意味着URI被更改,然后被重定向回处理管道并进行mod_rewrite.因此,将再次应用相同的规则范围,并且如果其中一个规则匹配并被应用,则同一件事再次发生,规则将再次循环.您可以在vhost/server配置中设置一个名为LimitInternalRecursion的指令,该指令设置这些内部重定向的限制.如果重写引擎循环(即重定向回自身)的次数超过此限制(我认为默认值为10),则将收到500 Internal Server错误.

During the URL processing pipeline, apache attempts to map a URL to a file or resource. Lots of different modules have a part to play in the processing pipeline, like mod_rewrite or mod_proxy or mod_alias. At any point, this URI can change, be flagged to be redirected, be flagged to be proxied, be flagged to throw an error, etc. When the URI gets to mod_rewrite, the rewrite engine collects a bunch of rules from the vhost config and the appropriate htaccess file; note, 2 different scopes here. Each scope of rules are applied to the URI and if none of the rules match, then mod_rewrite is done. If one of the rules match, there is an internal redirect, meaning the URI is changed and then redirected back to the processing pipeline and mod_rewrite. Thus the same scope of rules get applied again, and if one of the rules match and gets applied, the same thing happens again the the rules loop again. There's a directive that you can set in the vhost/server config called LimitInternalRecursion which sets the limit of these internal redirects. If the number of times the rewrite engine loops (i.e. redirects back to itself) exceeds this limit (which by default is 10, I think), then you get a 500 Internal Server error.

这听起来有些奇怪,但是有很多实例想要这样做.示例:从URI中删除所有_并替换为-:

This might sound kind of weird but there's a lot of instances for wanting to do this. Example: remove all _ from URI and replace with -:

RewriteRule ^(.*)_(.*)$ /$1-$2 [L]

如果URI是/a_b_c_d_foo,那么第一次,URI被更改为/a_b_c_d-foo,然后循环并被更改为`/a_b_c-d-foo,然后又被更改为/a_b-c-d-foo,直到您第五次得到/a-b-c-d-foo. >.它会再次循环,但是由于^(.*)_(.*)$模式不匹配,因此URI会通过重写引擎,并且循环会停止.

If the URI is /a_b_c_d_foo then the first time, the URI gets changed to /a_b_c_d-foo, then it loops and gets changed to `/a_b_c-d-foo, then again /a_b-c-d-foo and by the 5th time around you get /a-b-c-d-foo. It'll loop once more but since the ^(.*)_(.*)$ pattern doesn't match, the URI passes through the rewrite engine and the looping stops.

当人们创建不考虑循环的规则时,就会出现问题,例如:将/<anything>重写为/foo/<anything>:

The problem arises when people create rules that don't take into account the looping, for example: rewrite /<anything> to /foo/<anything>:

RewriteRule ^(.*)$ /foo/$1 [L]

如果URI为/bar,则URI第一次被重写为/foo/bar,这是所需的结果.但是URI在内部被重定向回重写引擎,并且再次匹配了相同的规则,从而导致:/foo/foo/bar,然后再次:/foo/foo/foo/bar,然后再次是:/foo/foo/foo/foo/bar,直到达到内部递归限制,您将获得500服务器错误.

If the URI is /bar then the first time the URI gets rewritten to /foo/bar, and this is the desired result. But the URI gets internally redirected back into the rewrite engine and the same rule is matched again, resulting in: /foo/foo/bar, then again: /foo/foo/foo/bar, and again: /foo/foo/foo/foo/bar, until the internal recursion limit is reached and you get a 500 Server Error.

这篇关于多个.htaccess文件中的多个mod_rewrite规则的优先顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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