mod_alias AliasMatch Regex - 匹配文件夹中除两个模式之外的所有内容?

发布于 2024-08-02 21:04:45 字数 447 浏览 9 评论 0原文

我想使用 AliasMatch 为文件夹中的所有内容创建别名,两个(或更多)特定正则表达式模式除外。

例如,以下 AliasMatch 为“content”文件夹中的所有内容创建别名:

AliasMatch /content(.*) /home/username/public_html/$1

但是有两个正则表达式模式我不希望上述 Alias 匹配,例如:

^content/([a-zA-Z0-9_-]+)/id-([a-zA-Z0-9_-]+)/([0-9]+)
^content/([a-zA-Z0-9_-]+)/nn-([a-zA-Z0-9_-]+)

我知道可以使用 NOT (!) 字符否定一个模式,但我不知道如何在这里使用它,也不知道如何在 AliasMatch 中否定多个模式。

这怎么可能做到呢?

I'd like to use AliasMatch to create an alias for everything within a folder, except for two (or more) specific regex patterns.

For instance the following AliasMatch creates an alias for everything in the 'content' folder:

AliasMatch /content(.*) /home/username/public_html/$1

But there are two regex patterns that I don't want the above Alias to match, for instance:

^content/([a-zA-Z0-9_-]+)/id-([a-zA-Z0-9_-]+)/([0-9]+)
^content/([a-zA-Z0-9_-]+)/nn-([a-zA-Z0-9_-]+)

I know that the NOT (!) character can be used to negate a pattern, but I don't know how to use it here, or how to negate multiple patterns in AliasMatch.

How could this be done?

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

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

发布评论

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

评论(2

舞袖。长 2024-08-09 21:04:45

ModAlias 实际上支持负向前瞻。我在我的网站上使用它,效果非常好:

AliasMatch ^/(?!w/|BingSiteAuth\.xml$|favicon\.ico$|google.{16}\.html$|robots\.txt$) /path/to/file

请确保您的正则表达式以“/”开头,因为它始终是 mod_alias 的第一个字符!切勿尝试以前瞻开始规则,否则解析将失败。

ModAlias DOES in fact support negative lookaheads. I'm using this on my site, which works very well:

AliasMatch ^/(?!w/|BingSiteAuth\.xml$|favicon\.ico$|google.{16}\.html$|robots\.txt$) /path/to/file

Be sure to have your regex start with a "/", as it is always the first character with mod_alias! Never try to start your rule with a lookahead, or else parsing will fail.

有深☉意 2024-08-09 21:04:45

您所说的称为负向先行,您只需将其包裹在正则表达式中以获取您不想匹配的内容,如下所示:(?!foo)。组合正则表达式可以很简单,只需将它们用管道连接在一起即可,但在本例中您可以做得更好一点。此正则表达式重用了两个正则表达式的第一部分,该部分是相同的:

[a-zA-Z0-9_-]+/(?:id-[a-zA-Z0-9_-]+/[0-9]+|nn-[a-zA-Z0-9_-]+)

因为管道(或 '|',交替运算符)的优先级低于其他任何内容,所以交替必须包含在团体。请注意,我使用了一个非捕获组——即(?:...)——并去掉了正则表达式中的括号。否则,他们会丢弃您想要捕获的一组的编号,并且您将不得不在第二部分中使用除 $1 之外的其他内容。规则。这是整个正则表达式:

^/content(?![a-zA-Z0-9_-]+/(?:id-[a-zA-Z0-9_-]+/[0-9]+|nn-[a-zA-Z0-9_-]+))(.*)

编辑:显然,AliasMatch 使用的正则表达式风格不支持前瞻,但有自己的否定语法:!(^/foo)。它的目的似乎是否定整个正则表达式,这意味着它不会帮助你,但也许你不需要它。也许您可以将这些目录别名为它们自己。那么你就不必否定任何事情。

AliasMatch ^(/content/[a-zA-Z0-9_-]+/id-[a-zA-Z0-9_-]+/[0-9]+.*) $1
AliasMatch ^(/content/[a-zA-Z0-9_-]+/nn-[a-zA-Z0-9_-]+.*) $1
AliasMatch ^/content/(.*) /home/username/public_html/$1

或者,您可以使用 指令或切换到 mod_rewrite 来执行某些操作。但我(显然)不是 Apache 专家——我的专长是正则表达式,而且我不认为正则表达式能够解决您的问题。

What you're talking about is called negative lookahead, and you just wrap it around the regex for what you don't want to match, like this: (?!foo). Combining regexes can be as simple as string them together with a pipe between them, but you can do a little better than that in this case. This regex reuses the first part of the two regexes, which is identical:

[a-zA-Z0-9_-]+/(?:id-[a-zA-Z0-9_-]+/[0-9]+|nn-[a-zA-Z0-9_-]+)

Because the pipe (or '|', the alternation operator) has lower precedence than anything else, the alternation has to be contained in a group. Notice that I used a non-capturing group -- i.e., (?:...) -- and got rid of the parentheses in your regexes. Otherwise, they would have thrown off the numbering of the one group you do want to capture, and you would have had to use something other than $1 in the second part of the rule. Here's the whole regex:

^/content(?![a-zA-Z0-9_-]+/(?:id-[a-zA-Z0-9_-]+/[0-9]+|nn-[a-zA-Z0-9_-]+))(.*)

EDIT: Apparently, the regex flavor used by AliasMatch doesn't support lookaheads, but has its own negation syntax: !(^/foo). Its purpose seems to be to negate the whole regex, which means it wouldn't help you, but maybe you don't need it. Maybe you can just alias those directories to themselves. Then you wouldn't have to negate anything.

AliasMatch ^(/content/[a-zA-Z0-9_-]+/id-[a-zA-Z0-9_-]+/[0-9]+.*) $1
AliasMatch ^(/content/[a-zA-Z0-9_-]+/nn-[a-zA-Z0-9_-]+.*) $1
AliasMatch ^/content/(.*) /home/username/public_html/$1

Or maybe you can do something with a <DirectoryMatch> directive, or by switching to mod_rewrite. But I'm (obviously) not an Apache expert--my specialty is regexes, and I don't think a regex is going to solve your problem.

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