Apache 配置:正则表达式禁止访问以点开头的文件/目录

发布于 2024-10-06 15:08:37 字数 392 浏览 0 评论 0原文

我想禁止访问名称以点开头的任何文件或目录。我想出了以下方法,但只有当它们直接位于文档根目录中时,它才会禁用对以 DOT 开头的文件/目录的访问。

<Files ~ "^\.|\/\.">
    Order allow,deny
    Deny from all
</Files>

有了这个,

http://my_server.com/.svn/entries   --> Permission denied
http://my_server.com/abcd/.svn/entries  --> Accessible, should be disabled

实现这个目标的正确正则表达式是什么?

I want to disable access to any file OR directory, whose name begins with a DOT. I came up with the following, but it disables access to files/directories beginning with DOT only if they are directly in the Document root.

<Files ~ "^\.|\/\.">
    Order allow,deny
    Deny from all
</Files>

With this,

http://my_server.com/.svn/entries   --> Permission denied
http://my_server.com/abcd/.svn/entries  --> Accessible, should be disabled

Whats the proper regex to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏花。依旧 2024-10-13 15:08:37

您的代码不起作用,因为 仅适用于所请求文档的basename。即最后一个斜杠之后的部分。 (来源:http://httpd.apache.org/docs/current/mod/core.html #files

Apache 在默认安装中阻止 .htaccess 文件(更好:所有以 .ht 开头的文件)。如果仔细查看配置文件,您会看到类似这样的内容:

<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
</FilesMatch>

因此,要隐藏以点开头的所有文件,您可以使用:

<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

为了使其适用于以点开头的目录,请使用以下(经过测试的)代码:

<DirectoryMatch "^\.|\/\.">
    Order allow,deny
    Deny from all
</DirectoryMatch>

You code does not work because <Files> only applies to the basename of the requested document. That is, the part after the last slash. (source: http://httpd.apache.org/docs/current/mod/core.html#files)

Apache blocks .htaccess files in a default installation (better: all files starting with .ht). If you looked closely at the configuration files, you would see something like this:

<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
</FilesMatch>

So to hide all files starting with a dot, you would use:

<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

In order to make it work for directories starting with a dot, use the following (tested) code:

<DirectoryMatch "^\.|\/\.">
    Order allow,deny
    Deny from all
</DirectoryMatch>
芯好空 2024-10-13 15:08:37
RewriteRule (^\.|/\.) - [F]

这将拒绝查看任何以点开头的文件或目录。
它影响路径树中的任何级别。

末尾的 [F] 修饰符表示禁止访问(不需要 L 修饰符来表示这是要应用的最后一条规则,默认情况下是隐含的)。

正则表达式有两个部分(其中任何一个都允许匹配,不需要两者都匹配):

(part1|part2)

第一部分匹配以点开头的任何内容(对于在每个目录的 .htaccess 文件中使用它时的情况,将不会有我们匹配的字符串开头的斜杠):

^\.

例如,这适用于 .test.git/HEAD
但不适用于 /.gitpath/.hidden

第二部分匹配任何包含斜杠后跟点的内容。如果您在 VirtualHost 或 side-wide Apache 配置中具有此规则,则这非常有用,在这种情况下,我们匹配的大小写字符串可能以斜杠开头。

此规则将匹配:/.gitsome/.hidden
此规则将不匹配:.git.hidden

当我们将这两个规则结合起来时,似乎我们涵盖了所有可能的情况。

RewriteRule (^\.|/\.) - [F]

This will deny from viewing any files or directories beginning with dot.
It affects any level in the paths tree.

[F] modifier at the end says to forbid access (no need for L modifier to say that it is Last rule to apply, it is implied by default).

The regular expression has two parts (any of them allowed to match, not required to be both):

(part1|part2)

The first part matches anything that starts from dot (for the case when you use it in per-directory .htaccess file and there will be no slash at the start of string we are matching on):

^\.

For example, this will work for .test, .git/HEAD
but will not work for /.git, path/.hidden.

The second part matches anything that contains slash followed by dot. This is useful if you have this rule in VirtualHost or in side-wide Apache configuration, in which a case string we match may begin with slash.

This rule will match: /.git, some/.hidden
This rule will not match: .git, .hidden

When we combine these both rules, it seems that we cover all possible cases.

所谓喜欢 2024-10-13 15:08:37

这对我来说非常有效(Apache 2.4):

<LocationMatch "\/\.">
    Require all denied
</LocationMatch>

拒绝任何包含以点开头的组件(文件或目录)的 URL。

This works perfectly for me (Apache 2.4):

<LocationMatch "\/\.">
    Require all denied
</LocationMatch>

Denies any URL that contains a component beginning with a dot (file or directory).

甜点 2024-10-13 15:08:37

使用重写模式:

RewriteEngine on

RewriteCond %{THE_REQUEST} ^.*/\.
RewriteRule ^(.*)$ - [R=404]

每个以点开头的文件或目录将被重定向到 404。

/myDir/.svn => 404
/.gitignore => 404
/dir1/dir2/dir3/..../.toHide => 404

With rewrite mod:

RewriteEngine on

RewriteCond %{THE_REQUEST} ^.*/\.
RewriteRule ^(.*)$ - [R=404]

Every file or dir who begin with a dot will be redirected to 404.

/myDir/.svn => 404
/.gitignore => 404
/dir1/dir2/dir3/..../.toHide => 404
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文