Apache 2.0 RewriteRule 正则表达式中不可避免的时期

发布于 2024-12-21 23:29:34 字数 962 浏览 2 评论 0原文

因此,我有以下正则表达式。

RewriteRule ^([-a-z0-9]*[A-Z\.]+.*)$ file.php?string=$1 [QSA]

我想要 file.php 触发的 URL 必须包含大写字母或句点,然后将该 URL 发送到 PHP 脚本。

但是,我遇到的问题是,由于未真正转义的句点,该脚本会在任何 URL 上触发。

我尝试过用一个反斜杠、两个反斜杠或三个反斜杠转义句点……但都无法阻止通用解释。

我做错了什么?

编辑:例如,

RewriteRule ^([-a-z0-9]*[A-Z\\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]

不起作用,但

RewriteRule ^([-a-z0-9]*\\.+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]

可以转义它。

编辑2:我想要重定向的网址示例:

  • /some-page-goes-here.html
  • /heres-Robs/Old/Page/

以及我不想重定向的网址:

  • /testing/one/two //an/actual-file.gif

编辑 3: 旧的正则表达式是:

RewriteRule ^([-a-z0-9]*[A-Z\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]

但是在撰写帖子时,我将问题的正则表达式更新为您在上面看到的内容

So, I have the following RegEx..

RewriteRule ^([-a-z0-9]*[A-Z\.]+.*)$ file.php?string=$1 [QSA]

The URL I want file.php to trigger for must either have capital letters or a period in it, then send the URL to the PHP script.

However, the problem I have is that this script is triggering on any URL, because of the not-truly-escaped Period.

I've tried escaping the period with a backslash, or two backslashes, or three... but none stop the generic interpretation.

What am I doing wrong?

Edit: As an example,

RewriteRule ^([-a-z0-9]*[A-Z\\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]

Doesn't work, but

RewriteRule ^([-a-z0-9]*\\.+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]

does escape it.

Edit 2: Examples of URLs I want to redirect:

  • /some-page-goes-here.html
  • /heres-Robs/Old/Page/

And ones I don't:

  • /testing/one/two/
  • /an/actual-file.gif

EDIT 3: Old regex was:

RewriteRule ^([-a-z0-9]*[A-Z\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]

But while writing the post, I updated the question's regex to what you see above.

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

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

发布评论

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

评论(2

追风人 2024-12-28 23:29:34

尝试:

RewriteCond %{REQUEST_URI} [A-Z] [OR]
RewriteCond %{REQUEST_URI} \.html$
RewriteRule (.*) file.php?string=$1 [QSA]

当使用 mod_rewrite 并且您有多个要匹配的 URL 时,最好使用 RewriteCond 进行过滤,然后应用您的 RewriteRule

Try:

RewriteCond %{REQUEST_URI} [A-Z] [OR]
RewriteCond %{REQUEST_URI} \.html$
RewriteRule (.*) file.php?string=$1 [QSA]

When using mod_rewrite and you have several URLs to match, it is always better to use RewriteCond to filter and then apply your RewriteRule.

旧人哭 2024-12-28 23:29:34

我认为您的问题可能不是您所想的那样:字符类中的句点应该表示字面句点,而不是“任何字符”。如果这确实是问题所在,那么您可以将 [AZ\.]+ 更改为 ([AZ]|\.)+ ;但我对此表示怀疑。需要尝试的一些事情:

  • 如果注释掉这一行会发生什么?这是否成功禁用此重定向?如果不是,那么显然问题不在于这一行。 :-)
  • 如果您通过将 QSA 更改为 QSA,R 来使其成为真正的 HTTP 重定向,会发生什么?目标 URL 是否符合您的预期?也许有一些意想不到的句点或大写字母? (警告:如果您在浏览器中尝试,这很可能会触发无限重定向循环;尝试通过端口 80 Telnet 提交请求并查看实际的 HTTP 响应可能会更容易。)

此外,您的规则不会不太符合你的描述。例如,您的规则不会匹配像 abc 这样的 URL,因为您只在单个“块”中出现大写字母和/或点;如果它们以小写字母分隔,则不会发生匹配。这只是因为您不想使描述过于复杂吗?

I don't think your problem can be what you think it is: periods in a character class are supposed to mean literal periods, not "any character". If this really is the problem, somehow, then you could change [A-Z\.]+ to ([A-Z]|\.)+; but I doubt it. Some things to try:

  • what happens if you comment out this line? does that successfully disable this redirect? if not, then obviously the problem isn't with this line. :-)
  • what happens if you make this a real HTTP redirect, by changing QSA to QSA,R? Does the destination URL look like what you expect? Maybe there are some unexpected periods or uppercase letters? (Warning: this will very likely trigger an infinite redirect loop if you try it in a browser; it'll probably be easier to try submitting the request via port-80 Telnet and seeing the actual HTTP response.)

Also, your rule doesn't quite match how you describe it. For example, your rule wouldn't match a URL like a.b.c, because you only uppercase letters and/or dots to occur in a single "clump"; if they're separated by lowercase letters, no match will occur. Is that just because you didn't want to overcomplicate the description?

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