有人可以解释这个奇怪的 mod_rewrite 正则表达式行为

发布于 2024-09-29 09:57:07 字数 797 浏览 0 评论 0原文

我一直在编写一个用于调试 mod_rewrite 的脚本,在测试他们的正则表达式系统时,我得到了一些奇怪的结果。我想知道这是否是 mod_rewrite 正则表达式引擎的正常行为,或者我的代码中的某些部分是否导致了它。

请求的 URL:http://myurl.com/path/to/something

.htaccess 有:RewriteRule to where

使用我的调试系统,以下是 RewriteRule 时发生的情况使用:

路径/到/某物 ->哪里/去/某事

不应该是path/where/something吗???

这是完整的 .htaccess 文件

重写引擎开启

RewriteBase /ModRewriteTester

重写规则.* - [E=ORIG:$0]

重写规则到哪里

重写规则.* - [E=MODD:$0]

RewriteRule.*index.php

然后我有一个在环境变量中读取的 php 脚本$_SERVER['REDIRECT_ORIG']$_SERVER['REDIRECT_MODD'],这就是我获取前面所述路径的地方。

如果有人知道更好的方法来明确展示 mod_rewrite 的正则表达式引擎如何工作,我对此持开放态度。最初的问题仍然存在......

I've been working on a script for debugging mod_rewrite, and when testing their regex system I've had some strange results. I'm wondering if this is normal behavior for the mod_rewrite regex engine or if some part in my code is causing it.

Requested URL: http://myurl.com/path/to/something

.htaccess has: RewriteRule to where

Using my debugging system, the following is what happens when that RewriteRule is used:

path/to/something -> where/to/something

Shouldn't it be path/where/something???

Here's the full .htaccess file

RewriteEngine On

RewriteBase /ModRewriteTester

RewriteRule .* - [E=ORIG:$0]

RewriteRule to where

RewriteRule .* - [E=MODD:$0]

RewriteRule .* index.php

Then I've got a php script that's reading in the environmental variables $_SERVER['REDIRECT_ORIG'] and $_SERVER['REDIRECT_MODD'], that's where I'm getting the previously stated paths.

If anyone knows a better way to explicitly show how mod_rewrite's regex engine works I'm open to it. The initial question still stands though...

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

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

发布评论

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

评论(1

楠木可依 2024-10-06 09:57:07

您的规则:

RewriteRule to where

...将重写与 to 匹配的 URL,并将其替换为表示对 /where 的请求的 URL。在某些情况下,mod_rewrite 可能会尝试重新添加 Apache 认为的内容 PATH_INFO可能创建如下情况:

path/to/somewhere  -> PATH_INFO = /to/somewhere
path/to/somewhere  -> /where
(append PATH_INFO) -> /where/to/somewhere

要检查您的场景中是否存在这种情况,您可以添加 DPI 标志RewriteRule 以丢弃 PATH_INFO(如果它)存在。看起来像这样:

RewriteRule to where [DPI]

在这种情况下,您最终会得到 URL /where。如果您想将 to 替换为 where,同时保留 URL 的其余部分,则需要一个更像这样的规则:

RewriteRule (.*?/)?to(/.*)? $1where$2

就调试规则集而言,如果您可以访问 Apache 配置,最好使用 具有足够高的 RewriteLogLevel 的 RewriteLog 指令。如果您无权访问该配置,那么您几乎只能执行与现在尝试执行的操作类似的操作。

Your rule:

RewriteRule to where

...will rewrite a URL that matches to and replace it with the URL representing what would be a request to /where. It's possible in certain circumstances for mod_rewrite to try and re-add what Apache believes to be PATH_INFO, which could create a situation like the following:

path/to/somewhere  -> PATH_INFO = /to/somewhere
path/to/somewhere  -> /where
(append PATH_INFO) -> /where/to/somewhere

To check if this is the case in your scenario, you can add the DPI flag to the RewriteRule to discard the PATH_INFO if it exists. This would look like this:

RewriteRule to where [DPI]

In this case, you would end up with just the URL /where. If you wanted to replace to with where while retaining the rest of the URL, you would need a rule more like this:

RewriteRule (.*?/)?to(/.*)? $1where$2

As far as debugging your rule set goes, if you have access to the Apache configuration you're much better off using the RewriteLog directive with a sufficiently high RewriteLogLevel. If you don't have access to the configuration, you're pretty much limited to doing something similar to what you're trying to do now.

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