开发者

Can someone explain this strange mod_rewrite regex behavior

开发者 https://www.devze.com 2023-01-22 03:57 出处:网络
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

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

Us开发者_如何学编程ing 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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消