mod_rewrite 使用 QSA,N 一起

问题描述 投票:0回答:1

我编写了一个 mod_rewrite 脚本,该脚本从任何查找中删除不存在的文件夹,但将文件夹名称传递给数组中的 .php 脚本。该脚本的工作原理如下:

RewriteEngine On

LogLevel alert rewrite:trace3

# Does the current request point to a file/folder that doesn't exist?
RewriteCond  /var/www/vhosts/example.org/httpdocs/fswtest%{REQUEST_FILENAME} !-f
RewriteCond  /var/www/vhosts/example.org/httpdocs/fswtest%{REQUEST_FILENAME} !-d
# Is there a folder in the URL? Get the first one in the list.
RewriteCond %{REQUEST_URI} \/([^\/]+)\/
# Remove JUST THE FIRST FOLDER from the REQUEST_URI and start again
RewriteRule .\/(.*) /$1?folder[]=%1 [N,QSA]

我的正则表达式工作得很好,它会从左侧开始每个循环删除 1 个文件夹,只要该文件夹不存在,直到剩下的都是正确的文件夹(我检查了日志以确保它正在执行此操作并且确实匹配)每次每个文件夹),但是,浏览到 http://example.org/this/folder/isnt/real/whatever.php 我期望 $_GET['folder'] 是一个 ['this', 'folder','isnt','real'] 但是由于某种原因,数组只包含 ['this','this','this','this'] 因为某些原因 %1 变量没有更新每个后续 [N] 个循环。

为什么 %1 在运行中等于“this”,而“this”一词由于在上一次运行中被删除而不再出现在 URI 中?

我尝试浏览http://example.org/this/folder/isnt/real/whatever.php

我希望通过 $_GET['folder'] = ['this','folder','isnt','real'] 重定向到“http://example.org/whatever.php” 相反,我被重定向到“http://example.org/whatever.php” $_GET['folder'] = ['this','this','this','this']

php apache mod-rewrite
1个回答
0
投票

好吧,事实证明,当您使用 [N] 循环回到更新后的请求开头时,%{REQUEST_URI} 实际上并未更新,因此与 %{REQUEST_URI} 的任何匹配都不会考虑对请求的更改已经做了。如果有任何方法可以对已使用 RewriteRule 修改的请求执行匹配,我不知道。

最后,我不得不将整个 URI 放入查询字符串中,并通过不同的方法对信息进行操作。

© www.soinside.com 2019 - 2024. All rights reserved.