htaccess的允许访问文件,推而广之?推而广之、文件、htaccess

2023-09-02 00:24:03 作者:宁乡小王子

我看见几个例子htaccess的禁用某些文件来访问:

I saw several htaccess example disabling some files to access:

<Files ~ ".(js|sql)$">
   order deny,allow
   deny from all
</Files>

例如,该prevents访问所有.JS和.SQL文件,其他人都启用。我想恰恰相反!我想这些文件被激活,所有其他被pvented $ P $。如何实现这一目标?

for example, this prevents to access all .JS and .SQL files, the others are enabled. I want the contrary! I want those files to be ENABLED, all others to be prevented. How to achieve this?

推荐答案

Vorapsak的答案几乎是正确的。它实际上

Vorapsak's answer is almost correct. It's actually

order allow,deny
<Files ~ ".(js|sql)$">
   allow from all
</Files>

您需要的顺序指令上方(和你不需要任何东西)。

You need the order directive at the top (and you don't need anything else).

有趣的是,似乎我们不能否定在FilesMatch,这是......奇怪的正则表达式,特别是因为!不会造成服务器错误或任何东西。嗯,真不错。

The interesting thing is, it seems we can't just negate the regex in FilesMatch, which is... weird, especially since the "!" causes no server errors or anything. Well, duh.

和一些说明:

订单原因讲述了它的预期违约行为的服务器中。在

The order cause tells the server about its expected default behaviour. The

 order allow,deny

告诉服务器先处理允许指令:如果请求符合任何允许指令,它被标记为好。那么拒绝指令evaulated:如果一个请求匹配任何拒绝指令,它否认(这不要紧,如果它是在第一遍允许)。如果没有找到匹配,该文件将被拒绝。

tells the server to process the "allow" directives first: if a request matches any allow directive, it's marked as okay. Then the "deny" directives are evaulated: if a request matches any deny directives, it's denied (it doesn't matter if it was allowed in the first pass). If no matches were found, the file is denied.

该指令

 order deny,allow

工作方式恰好相反:先在服务器处理拒绝指令:如果一个请求匹配,它标志着予以否认。那么允许指令evaulated:如果一个请求匹配的允许指令,它是允许的,即使它拒绝指令先前匹配。如果请求没有匹配时,该文件被允许。

works the opposite way: first the server processes the "deny" directives: if a request matches, it's marked to be denied. Then the "allow" directives are evaulated: if a request matches an allow directive, it's allowed in, even if it matches a deny directive earlier. If a request matches nothing, the file is allowed.

在这种特定情况下,服务器首先尝试匹配允许指令:它看到JS和SQL文件是允许的,因此请求foo.js经历;请求bar.php比赛没有指令,因此它拒绝。

In this specific case, the server first tries to match the allow directives: it sees that js and sql files are allowed, so a request to foo.js goes through; a request to bar.php matches no directives, so it's denied.

如果我们交换指令以订单拒绝,允许,然后foo.js会经过(为是一个JS),并bar.php也会通过,因为它没有匹配的模式。

If we swap the directive to "order deny,allow", then foo.js will go through (for being a js), and bar.php will also go through, as it matches no patterns.

哦,并且一件事:指令中的部分的(即&LT;文件>和&lt;目录>)总是evaulated的的的主体之后。 htaccess文件,覆盖它。这就是为什么Vorapsak的解决方案inteded没有工作:主要的.htaccess拒绝了这一要求,那么在&lt;文件>订单处理,并允许该请求。

oh and, one more thing: directives in a section (i.e. < Files> and < Directory>) are always evaulated after the main body of the .htaccess file, overwriting it. That's why Vorapsak's solution did not work as inteded: the main .htaccess denied the request, then the < Files> order was processed, and it allowed the request.

.htaccess的是最糟糕的一种魔力,但有逻辑的。

Htaccess is magic of the worst kind, but there's logic to it.