Apache RewriteRules - 什么时候不使用[L]标志?

Bad*_*sie 9 .htaccess mod-rewrite

我知道[L]标志应该停止正在处理的任何进一步的RewriteRules但我似乎在每个RewriteRule中添加[L],这是代码片段中几乎所有规则的一部分.在线示例.

我真的不明白你何时愿意并且不想使用[L]标志,因为看起来我写的所有RewriteRules都可以使用或不使用[L]标志.

有人可以提供一个需要[L]的规则和一个不应该有[L]的规则的例子吗?

Jon*_*Lin 12

当你不想使用的时候,我不知道如何L:

  • 当你需要用N标志循环重写时

    # Replace all instances of "aaa" with "zzz"
    RewriteRule ^(.*)aaa(.*)$ /$1zzz$2 [N]
    
    Run Code Online (Sandbox Code Playgroud)
  • 当您使用C标志在逻辑上链接一组重写时

    # replace "old" with "new" but only if the first rule got applied
    RewriteRule ^special/(.*)$ /chained/$1 [C]
    RewriteRule ^(.*)/old/(.*)$ /$1/new/$2 [L]
    
    Run Code Online (Sandbox Code Playgroud)
  • 当你需要使用S标志跳过一些规则时(取自apache docs,因为我无法想到一个工作示例)

    # Is the request for a non-existent file?
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # If so, skip these two RewriteRules
    RewriteRule .? - [S=2]
    
    RewriteRule (.*\.gif) images.php?$1
    RewriteRule (.*\.html) docs.php?$1
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果要重定向但需要让其他规则在使用R标志重定向之前处理URI

    # If host is wrong, redirect
    RewriteCond %{HTTP_HOST} bad.host.com
    RewriteRule ^stuff/(.*)$ http://good.host.com/$1 [R=301]
    
    # but not until we apply a few more rules
    RewriteRule ^(.*)/bad_file.php$ /$1/good_file.php [L]
    
    Run Code Online (Sandbox Code Playgroud)

另一方面,有时L不需要使用标志,但它确保在应用该规则时重写停止.它只是让规则变得更容易,因为它使不同的规则集不会相互干扰,所以通常在安全方面的事情就是L在所有规则的最后无害地包含一个标志,因为99%的那个时候,你真的只想停在那里.

请注意,L仅停止当前重写的迭代.重写引擎将循环,直到进入引擎的URI与出现的URI完全相同(查询字符串不是URI的一部分).