Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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
Apache htaccess多个if/or规则的规则,包括用户代理、cookie、uri和文件名_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Apache htaccess多个if/or规则的规则,包括用户代理、cookie、uri和文件名

Apache htaccess多个if/or规则的规则,包括用户代理、cookie、uri和文件名,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我有以下.htaccess规则。我需要给这个区块添加一些规则。我不想失去我的老朋友 <FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$"> Order allow,Deny Deny from all </FilesMatch> <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.

我有以下.htaccess规则。我需要给这个区块添加一些规则。我不想失去我的老朋友

<FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
Order allow,Deny
Deny from all
</FilesMatch>

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
我试过这个。但是没有帮助。我怎么写这些规则

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]

    RewriteCond %{HTTP_USER_AGENT} "BotOne|OtherBot" [NC,OR]
    RewriteCond %{HTTP_COOKIE} !^.*user_id=1   [NC]
    #
    RewriteCond %{REQUEST_URI} \/  [NC,OR]
    RewriteCond %{REQUEST_FILENAME} ^utm_source.*  [NC,OR]
    RewriteCond %{REQUEST_FILENAME} ^utm_medium.*  [NC,OR]
    RewriteCond %{REQUEST_FILENAME} ^utm_campaign.*  [NC,OR]
    RewriteCond %{REQUEST_FILENAME} ^utm_content.*  [NC]
    #
    RewriteCond %{REQUEST_FILENAME} !\/blog\/.*  [NC,OR]
    RewriteCond %{REQUEST_FILENAME} !gif.*  [NC,OR]
    RewriteCond %{REQUEST_FILENAME} !jpg.*  [NC]
    RewriteRule ^.*? index.html [R=301,L]
</IfModule>

重新启动发动机
重写条件%{HTTP_HOST}^www\.(.*)$[NC]
重写规则^(.*)$http://%1/$1[R=301,L]
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写规则^(.*)$index.php[QSA,L]
重写cond%{HTTP_USER_AGENT}“BotOne | OtherBot”[NC,或]
重写cond%{HTTP_COOKIE}!^*用户id=1[NC]
#
重写条件%{REQUEST_URI}\/[NC,或]
RewriteCond%{REQUEST_FILENAME}^utm_source.*[NC,或]
重写COND%{REQUEST_FILENAME}^utm_medium.*[NC,或]
重写COND%{REQUEST_FILENAME}^utm_活动。*[NC,或]
重写COND%{REQUEST_FILENAME}^utm_内容。*[NC]
#
重写cond%{REQUEST_FILENAME}\/博客\/.[NC,或]
重写cond%{REQUEST_FILENAME}!gif.*[NC,或]
重写cond%{REQUEST_FILENAME}!jpg.*[NC]
重写规则^.*?index.html[R=301,L]
我要重定向的主要URL如下:
*=>
*=>
*=>
*=>
*=>
**=>无重定向
*=>无重定向
*=>无重定向

如果(用户代理为“BotOne”)或(用户代理为“OtherBot”)或(他/她的Cookie用户\u id不是1),将触发此重定向


任何查询参数都将被删除。

在.htaccess中处理规则的方式,根本无法用某种类似于编程语言的构造或解析来表达这一点。在过去,我也有类似的问题,而且很难得到一个完整的答案,当我最终得到答案时,我自己写了下来,以便将来能再次找到它。以下是我给自己写的:

## After quite a bit of puzzlement and seemingly maddeningly
##  vague documentation, I finally figured out exactly how mod_rewrite's
##  [OR] flag really works: In mod_rewrite there's not really any
##  "precendence"; RewriteCond's are simply processed sequentially.
##  Without any modification, the default is to AND _everything_.
##  Including the [OR] modifier on some RewriteCond's creates a
##  two-level expression with only ANDs at the outer/upper level and
##  only ORs at the inner/lower level. Thus
##  RewriteCond a [OR]
##  RewriteCond b
##  RewriteCond c [OR]
##  RewriteCond d
##  RewriteCond e [OR]
##  RewriteCond f [OR]
##  RewriteCond g
##  is equivalent to the boolean expression
##  ((a OR b) AND (c OR d) AND (e OR f OR g))
## There's _no_ way to have ANDs at the _lower/inner_ level and ORs
##  at the _upper/outer_ level; such constructs can only be implemented with
##  either multiple rulesets (and unavoidable duplication), or the
##  introduction of intermediate environment variables.
## Thus the only advantages of [OR] over a | in an RE are increased
##  clarity/maintainability, and the possibility of checking against
##  unrelated variables. REs with lots of |, on the other hand, are
##  assumed to be much faster.
如果我正确理解你的需要,整个事情可以被认为是一个巨大的条件,与块连接的不是附属的“如果”条款,而是和,像这样:

- if HTTP_USER_AGENT includes BotOne
- or HTTP_USER_AGENT includes OtherBot
- or HTTP_COOKIE user_id != 1

    - if REQUEST_URI is "/" main directory
    - or REQUEST_FILENAME includes "utm_source"
    - or REQUEST_FILENAME includes "utm_medium"
    - or REQUEST_FILENAME includes "utm_campaign" and "utm_content"

        - if REQUEST_FILENAME doesn't include "/blog/"
        - or REQUEST_FILENAME doesn't include "gif"
        - or REQUEST_FILENAME doesn't include "jpg"

            - then RewriteRule all files to index.html
IF

((- HTTP_USER_AGENT includes BotOne
- or HTTP_USER_AGENT includes OtherBot
- or HTTP_COOKIE user_id != 1)
AND
(- REQUEST_URI is "/" main directory
- or REQUEST_FILENAME includes "utm_source"
- or REQUEST_FILENAME includes "utm_medium"
- or REQUEST_FILENAME includes "utm_campaign" and "utm_content")
AND
(- REQUEST_FILENAME doesn't include "/blog/"
- or REQUEST_FILENAME doesn't include "gif"
- or REQUEST_FILENAME doesn't include "jpg"))

THEN

- RewriteRule all files to index.html
RewriteCond "utm_campaign.*utm_content" [OR]
RewriteCond "utm_content.*utm_campaign"
我所看到的最大的复杂性是关于“utm_活动”和“utm_内容”的规则,因为据我所知,正则表达式(即使是像.htaccess中那样复杂的PERL样式的表达式)根本不能很好地处理未指定的顺序。如果您知道字符串实际上总是以相同的顺序排列,那么您可以编写一个类似“utm_活动。*utm_内容”的RE。如果确实未指定顺序,为了完全满足您的规范,您需要两个规则条件,每个可能的顺序对应一个,如下所示:

- if HTTP_USER_AGENT includes BotOne
- or HTTP_USER_AGENT includes OtherBot
- or HTTP_COOKIE user_id != 1

    - if REQUEST_URI is "/" main directory
    - or REQUEST_FILENAME includes "utm_source"
    - or REQUEST_FILENAME includes "utm_medium"
    - or REQUEST_FILENAME includes "utm_campaign" and "utm_content"

        - if REQUEST_FILENAME doesn't include "/blog/"
        - or REQUEST_FILENAME doesn't include "gif"
        - or REQUEST_FILENAME doesn't include "jpg"

            - then RewriteRule all files to index.html
IF

((- HTTP_USER_AGENT includes BotOne
- or HTTP_USER_AGENT includes OtherBot
- or HTTP_COOKIE user_id != 1)
AND
(- REQUEST_URI is "/" main directory
- or REQUEST_FILENAME includes "utm_source"
- or REQUEST_FILENAME includes "utm_medium"
- or REQUEST_FILENAME includes "utm_campaign" and "utm_content")
AND
(- REQUEST_FILENAME doesn't include "/blog/"
- or REQUEST_FILENAME doesn't include "gif"
- or REQUEST_FILENAME doesn't include "jpg"))

THEN

- RewriteRule all files to index.html
RewriteCond "utm_campaign.*utm_content" [OR]
RewriteCond "utm_content.*utm_campaign"
在我看来,你的一些REs表达的东西和你的伪规则实际上说的不完全一样。例如:

REQUEST_FILENAME includes "utm_source"
应该成为

RewriteCond ${REQUEST_FILENAME} utm_source
因为

RewriteCond ${REQUEST_FILENAME} ^utm_source 
实际执行

REQUEST_FILENAME **startswith** utm_source
此外,我还允许使用奇怪的浏览器,它们根本不以任何形式发送根,如下所示(还要注意,“/”没有单独的大写和小写版本,因此[NC]只是在没有充分理由的情况下给您带来了轻微的性能影响)。请注意,您需要字符串锚的开头(“^”)和结尾(“$”),否则您也将匹配“/xxx/yyy/zzz”之类的内容,因为它们包含斜杠:

RewriteCond ${REQUEST_URI} ^/?$ [OR]
最后,只匹配你关心的那部分字符串;不需要匹配字符串的其余部分(事实上,尝试匹配字符串的其余部分通常会导致不必要的错误)。换句话说,在.htre中出现“*”通常表示某种不必要的奇怪,充其量会影响性能,最坏的情况下会掩盖一些错误。不要说“utm_源。*”,只要说“utm_源”

乍一看,我认为您的逻辑和多个条件是正确的(幸运的是,有很多方法可以让复杂的条件变得如此混乱)。因此,如果它不起作用,我会怀疑规则(特别是正则表达式)存在其他问题,而不是逻辑/优先级错误。(另外,我猜问题有几个不同的原因,而不仅仅是一个共同的根本原因,因此解决一个问题不可能解决所有其他问题。)


你能给我们一个输入字符串的具体例子,你期望发生什么,实际发生了什么

?例如,
utm\u source
在传入URL中的什么位置??这里:
http://example.com/utm_source
,此处:
http://example.com/folder/folder/folder/utm_source
,在哪里?这是不可能猜到的。请给出所有重定向条件的URL示例。对不起,我编辑了这个问题。