有人可以解释这个奇怪的 mod_rewrite 正则表达式行为
我一直在编写一个用于调试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的规则:
...将重写与 to 匹配的 URL,并将其替换为表示对 /where 的请求的 URL。在某些情况下,mod_rewrite 可能会尝试重新添加 Apache 认为的内容 PATH_INFO,可能创建如下情况:
要检查您的场景中是否存在这种情况,您可以添加
DPI
标志 到RewriteRule
以丢弃 PATH_INFO(如果它)存在。看起来像这样:在这种情况下,您最终会得到 URL /where。如果您想将 to 替换为 where,同时保留 URL 的其余部分,则需要一个更像这样的规则:
就调试规则集而言,如果您可以访问 Apache 配置,最好使用
具有足够高的
指令。如果您无权访问该配置,那么您几乎只能执行与现在尝试执行的操作类似的操作。RewriteLogLevel
的 RewriteLogYour rule:
...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:
To check if this is the case in your scenario, you can add the
DPI
flag to theRewriteRule
to discard the PATH_INFO if it exists. This would look like this: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:
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 highRewriteLogLevel
. 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.