在Apache中使用REGEX在配置文件和.htaccess中的指令中使用

发布于 2025-01-30 05:56:52 字数 1815 浏览 3 评论 0原文

如果我正确理解下一个代码中的表达式.ht*将与.ht开头的所有内容匹配,所以我的.ht_lalala是安全的。

<Files ".ht*">
    Require all denied
</Files>

但是下一个呢?

(^\。ht |〜$ |​​ back | back | backup | backup $)

匹配文件是否正确:.htaccessback back ,备份备份?或接下来会更好

(^\。ht*| back*| back*$)

我想了解的是〜$实际上是在我的代码中(以正则模式)。我不知道为什么以及何时将其放在那里,但是我的代码中有它,现在我怀疑这是正确的。


我知道有关正则的基本知识,什么是^$,而*表示0或n。 >〜在模式中没有意义,除非它只是一个简单的字符,并且除了之外,它无能为力。我已经阅读了Apache文档,我猜对于多个匹配文件匹配,目录match更好,但是正式表达式也可以在指令中使用:文件和目录,并添加〜字符,如文档示例中所述。

<Files ~ "\.(gif|jpe?g|png)$">
    #...
</Files>

好吧,我想要的是知道如何匹配不同的文件或目录。

还有一件事,我应该逃脱吗?因为默认的httpd.conf不这样做。或者对于httpd.conf和.htaccess(对我没有意义)的


更新

回答我自己的问题,我如何与Regex匹配此 .ht,.htacccess,.htpasswd,back,back,bac. ,返回,备份,备份,首先,我决定以(dot)的名义使用我想要隐藏的任何东西。其次,我发现简洁的模式^(\ ..*)$将完​​成工作,将为我提供所需的东西。或^\。 更好!所以,如果将来我想隐藏一些东西,我只在名称开始时添加

在这里,下一个代码将拒绝从网络访问任何名称以(测试,工作)开头的任何文件和目录

<FilesMatch "^\.">
    Require all denied
</FilesMatch>

<DirectoryMatch "^\.">
    Require all denied
</DirectoryMatch>

访问 a href =“ https://stackoverflow.com/a/722291982/10324990”> @mrwhite澄清和简化我的方法,所以我坚持使用此方法(已测试,works,works)

野外卡片字符串匹配:

<Files ".*">
    Require all denied
</Files>

<Directory ".*">
    Require all denied
</Directory>

If I understand correctly expression .ht* in the next code will match all that starts with .ht, so my .ht_lalala is safe.

<Files ".ht*">
    Require all denied
</Files>

But what about next one?

(^\.ht|~$|back|BACK|backup|BACKUP$)

Is it correct for matching files: .htaccess, back, backup, BACKUP? Or next will be better instead

(^\.ht*|back*|BACK*$)

What I'd like to understand is what ~$ actually means in my code (in RegEx pattern). I don't know why and when I put it there, but I have it in my code, and now I doubt that it's correct.


I know basic things about RegEx, what is ^ and $, and that * means 0 or N from previous text/token, but ~ doesn't make sense inside the pattern, unless it's just a simple character and it does nothing but literally matches ~. I've read Apache docs, I guess for multiple matches FilesMatch and DirectoryMatch is better, however regular expressions can also be used on directives: Files and Directory, with the addition of the ~ character, as is stated in the docs examples.

<Files ~ "\.(gif|jpe?g|png)
quot;>
    #...
</Files>

And well, what I want exactly is to know how to match different files or directories.

One more thing, should I escape the .? Because default httpd.conf doesn't do so. Or it's just different for httpd.conf and .htaccess (which doesn't make sense to me)


UPDATE

Answering to my own question, how do I match with RegEx any of this .ht, .htaccess, .htpasswd, back, BACK, backup, BACKUP, first at all I decided to use . (dot) in the name of anything I want to hide. Secondly, I found out that laconic pattern ^(\..*)$ will do the job, will give me what I need. Or ^\. even better! So, if in the future I would like to hide something, I just add the . at the start of the name.

Here we go, next code will deny access from the web to any files and directories which names start with . (tested, works)

RegEx pattern match:

<FilesMatch "^\.">
    Require all denied
</FilesMatch>

<DirectoryMatch "^\.">
    Require all denied
</DirectoryMatch>

And in brilliant explanation @MrWhite clarified and simplified my method, so I stuck with this (tested, works)

Wild-card string match:

<Files ".*">
    Require all denied
</Files>

<Directory ".*">
    Require all denied
</Directory>

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

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

发布评论

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

评论(2

时光倒影 2025-02-06 05:56:52
 &lt; files“ .ht*”&gt;
 

在这种情况下,.ht*不是正则表达式(REGEX)。它是一个“通用卡字符串”,其中与任何单个字符匹配,*匹配任何字符序列。 (虽然这也是有效的正则义务 - 正则态度会有所不同)。

但是下一个呢?

 (^\。ht |〜$ |​​ back | back | backup | backup $)
 

这是一个正则(在&lt; files&gt;指令中都无法使用,而无需启用与参数匹配的正格图案 - 如您所使用的那样稍后。)

在此正则时,〜$匹配任何以字面(tilde cartare)结尾的字符串。这有时用于标记备份文件。

它还匹配...

  • 任何启动.ht的字符串(自然包含.htaccess)。
  • 任何包含 back back back back> backup back back (匹配备份显然是多余的)的字符串)。
  • 任何以backup结尾的字符串。

因此,这看起来并不像您认为它在做的事情。

或下一个会更好

 (^\。ht*| back*| back*$)
 

这是有效的正则义务,显然您已经恢复了“通用卡”图案匹配的混合。请记住,在Regex说话中,*量词匹配上一个令牌0或更多次。它与“任何字符”不匹配,例如通配符模式匹配。

这仍然与“ .htaccess”匹配,但这仅仅是因为该模式没有锚定。例如,^\。ht*$(带有串线锚)将 not> not 匹配“ .htaccess”。

 &lt; files〜“ \。(gif | jpe?g | png)$”&gt;
 

使用文件指令,参数启用REGEX模式匹配。 (正如您所说的。)这与在Regex模式本身内使用时有很大不同。

另一件事,我应该逃脱吗?因为默认的httpd.conf不这样做。或者对于httpd.conf和.htaccess(对我没有意义)

是不同的

我认为您正在将事情混合在一起。在您的第一个示例中,它不是正则是“通用卡”模式(如上所述)。在这种情况下,不得反斜击esc。它匹配字面的(dot)。 在这里没有特殊的含义。 仅在需要匹配正则表达式中的字面点中时才能逃脱。

例如,以下内容是等效的:(

# Wild-card string match
<Files ".ht*">

但是

# Regex pattern match
<Files ~ "^\.ht">

,最好使用files -match而不是files〜避免任何混乱。Files> Files Match Match是 “较新”语法。

在这方面,


更新:

我发现laconic模式^(\ ..*)$将完​​成工作...

我们去,下一个代码将拒绝从网络访问任何文件,
名称的目录以开头(测试,工作)

 &lt; files -match“^(\ ..*)$”&gt;
    需要所有人拒绝
&lt;/filesmatch&gt;
 

可以简化。您无需从字面上匹配整个文件名。您只需要断言文件名以一个点开头(这要高得多)。因此,您不需要 capture (括号为子图案)文件名 - 您没有对此做任何事情。

要断言文件名从使用Regex的点开始,然后只需使用^\。 - 仅此而已。例如:

<FilesMatch "^\.">

请记住,默认情况下,正则量音量词(例如*)是 greedy ,因此您无需遵循。*在匹配文件名时,用串联锚定锚。因此,在此上下文中,Regex ^。*$。*实际上是相同的。两者都匹配整个文件名。 (在此上下文中没有新的字符。)

可以通过根本不使用正则表达式并使用带有香草&lt;文件&gt; Directive的通用卡字符串模式来进一步“简化”。例如,这与:

<Files ".*">

nb:这不是正则。这是一个字面的点,后跟任何数量的字符(通用卡语法)。

<Files ".ht*">

In this context, .ht* is not a regular expression (regex). It is a "wild-card string", where ? matches any single character, and * matches any sequence of characters. (Whilst this is also a valid regex - a regex would match differently).

But what about next one?

(^\.ht|~$|back|BACK|backup|BACKUP$)

This is a regex (it cannot be used in the <Files> directive as you have written above, without enabling regex pattern matching with the ~ argument - as you have used later.)

In this regex, ~$ matches any string that ends with a literal ~ (tilde character). This is sometimes used to mark backup files.

It also matches...

  • Any string that starts .ht (which naturally includes .htaccess).
  • Any string that contains back or BACK or backup (matching backup is obviously redundant).
  • Any string that ends with BACKUP.

Consequently, this does not look like it's doing quite what you think it's doing.

Or next will be better instead

(^\.ht*|back*|BACK*$)

Whilst this is a valid regex, you've obviously reverted back to a mix of "wild-card" pattern matching. Bear in mind that in regex speak, the * quantifier matches the previous token 0 or more times. It does not match "any characters", as in wild-card pattern matching.

This still matches ".htaccess", but only because the pattern is not anchored. For example, ^\.ht*$ (with an end-of-string anchor) would not match ".htaccess".

<Files ~ "\.(gif|jpe?g|png)
quot;>

With the Files directive, the ~ argument enables regex pattern matching. (As you've stated.) This is quite different from when ~ is used inside the regex pattern itself.

One more thing, should I escape the .? Because default httpd.conf doesn't do so. Or it's just different for httpd.conf and .htaccess (which doesn't make sense to me)

I think you're mixing things up. In your first example, it's not a regex, it's a "wild-card" pattern (as stated above). In this context, the . must not be backslash-escaped. It matches a literal . (dot). The . carries no special meaning here. The . should only be escaped if you need to match a literal dot in a regular expression.

For example, the following are equivalent:

# Wild-card string match
<Files ".ht*">

and

# Regex pattern match
<Files ~ "^\.ht">

(However, it is preferable to use FilesMatch instead of Files ~ to avoid any confusion. FilesMatch is "newer" syntax.)

There is no difference between httpd.conf and .htaccess in this regard.


UPDATE:

I found out that laconic pattern ^(\..*)$ will do the job ...

Here we go, next code will deny access from the web to any files and
directories which names start with . (tested, works)

<FilesMatch "^(\..*)
quot;>
    Require all denied
</FilesMatch>

This can be simplified. You do not need to literally match the entire filename. You simply need to assert that the filename starts with a dot (and this is much more efficient). Consequently, you do not need to capture (parenthesised subpattern) the filename - you are not doing anything with it.

To assert that the filename starts with a dot using regex, then just use ^\. - nothing more. For example:

<FilesMatch "^\.">

Bear in mind that regex quantifiers (eg. *) are greedy by default, so you do not need to follow a pattern like .* with an end-of-string anchor when matching a filename. So, the regex ^.*$ and .* are effectively the same in this context. Both match the entire filename. (There are no newline characters in this context.)

This can be further "simplified" by not using regex at all and using a wild-card string pattern with a vanilla <Files> directive. For example, this is the same as:

<Files ".*">

NB: This is not a regex. It is a literal dot followed by any number of characters (wild-card syntax).

相思碎 2025-02-06 05:56:52

apache手册涵盖了这一点。

启用Regex。没有它,您只需访问通配符*即可。

据我所知,Apache使用了Regex的PCRE风味。

因此,一旦您通过启用了正则态度,然后使用 https:// regex101。 com/r/lpkmhk/1 测试您编写的正则表达式的行为。

The Apache manual covers this.

~ enables regex. Without it, you just get access to wildcards ? and *.

As far as I know Apache uses the PCRE flavor of regex.

So once you've enabled regex via ~ then use https://regex101.com/r/lPkMHK/1 to test the behavior of the regex you've written.

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