Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess Apache重写规则-何时不使用[L]标志?_.htaccess_Mod Rewrite - Fatal编程技术网

.htaccess Apache重写规则-何时不使用[L]标志?

.htaccess Apache重写规则-何时不使用[L]标志?,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我知道[L]标志应该阻止任何正在处理的进一步重写规则,但我似乎理所当然地将[L]添加到每个重写规则中,这几乎是在线代码片段和示例中发现的所有规则的一部分 我真的不明白你什么时候愿意也不愿意使用[L]标志,因为我写的所有重写规则似乎都使用或不使用[L]标志 有人能提供一个需要[L]和不应该有[L]的规则的例子吗?在我脑海中,当你不想使用L时: 当需要使用N标志循环重写时 # Replace all instances of "aaa" with "zzz" RewriteRule ^(.*)aa

我知道[L]标志应该阻止任何正在处理的进一步重写规则,但我似乎理所当然地将[L]添加到每个重写规则中,这几乎是在线代码片段和示例中发现的所有规则的一部分

我真的不明白你什么时候愿意也不愿意使用[L]标志,因为我写的所有重写规则似乎都使用或不使用[L]标志


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

在我脑海中,当你不想使用
L
时:

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

    # Replace all instances of "aaa" with "zzz"
    RewriteRule ^(.*)aaa(.*)$ /$1zzz$2 [N]
    
    # replace "old" with "new" but only if the first rule got applied
    RewriteRule ^special/(.*)$ /chained/$1 [C]
    RewriteRule ^(.*)/old/(.*)$ /$1/new/$2 [L]
    
    # 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]
    
  • 当您使用
    C
    标志以逻辑方式将一组重写链接在一起时

    # Replace all instances of "aaa" with "zzz"
    RewriteRule ^(.*)aaa(.*)$ /$1zzz$2 [N]
    
    # replace "old" with "new" but only if the first rule got applied
    RewriteRule ^special/(.*)$ /chained/$1 [C]
    RewriteRule ^(.*)/old/(.*)$ /$1/new/$2 [L]
    
    # 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]
    
  • 当您需要使用
    S
    标志跳过某些规则时(取自apache文档,因为我想不出一个有效的示例)

  • 当您想要重定向但需要其他规则在重定向之前使用
    R
    标志处理URI时

    # Replace all instances of "aaa" with "zzz"
    RewriteRule ^(.*)aaa(.*)$ /$1zzz$2 [N]
    
    # replace "old" with "new" but only if the first rule got applied
    RewriteRule ^special/(.*)$ /chained/$1 [C]
    RewriteRule ^(.*)/old/(.*)$ /$1/new/$2 [L]
    
    # 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]
    
另一方面,有时不需要使用
L
标志,但它可以确保在应用该规则时停止重写。它只是让制定规则变得更容易,因为它可以防止不同的规则集相互干扰,所以通常在所有规则的末尾添加一个
L
标志是安全的,因为99%的时候,你真的只想停在那里

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