Regex 修改时行为异常。(点)在正则表达式中

Regex 修改时行为异常。(点)在正则表达式中,regex,mod-rewrite,apache2,Regex,Mod Rewrite,Apache2,我正在尝试将我站点的所有流量重定向到单个脚本,如下所示: example.com/fictitious/path/to/resource 应该成为 example.com/target.php?path=fictitious/path/to/resource 我已经设置了.htaccess文件,如下所示: RewriteEngine on RewriteBase "/" RewriteRule "^(.*)$" "target.php?path=$1" 出于测试目的,target.php如

我正在尝试将我站点的所有流量重定向到单个脚本,如下所示:

example.com/fictitious/path/to/resource
应该成为

example.com/target.php?path=fictitious/path/to/resource
我已经设置了.htaccess文件,如下所示:

RewriteEngine on
RewriteBase "/"
RewriteRule "^(.*)$" "target.php?path=$1"
出于测试目的,target.php如下所示:

<?php echo $_GET["path"] ?>
毫无疑问,target.php忠实地响应“path/to/resource”,但只要我在规则中添加一个转义点:

[...]
RewriteRule "^([a-zA-Z\/\.]*)$" "target.php?path=$1"
target.php再次回显“target.php”


发生了什么事?为什么正则表达式中的点会以这种方式干扰捕获组的内容?

问题是您的规则正在循环,因此运行了两次。在第一次执行
REQUEST\u URI
后变成
target.php
,在第二次执行中,在
path
参数中得到相同的结果

这是因为您没有任何条件可以避免对现有文件和目录运行此规则

您可以使用:

RewriteEngine on

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ target.php?path=$1 [L,QSA]

非常感谢。那么,我是否正确地假设它从用户请求中获取URL,重写它,然后从顶部开始为重写的URL启动链,直到URL不再更改?因为我之前的假设是它只在链中运行一次,只重写请求URL。是的,你的假设是正确的,它再次从顶部开始。有关更多详细信息,请参见:
RewriteEngine on

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ target.php?path=$1 [L,QSA]