我写了一个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]
我的正则表达式工作得很好,它从左边开始每次循环删除一个文件夹,只要文件夹不存在,直到剩下的都是正确的文件夹(我检查了日志,以确保它正在这样做,确实它每次都匹配每个文件夹),然而,浏览到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中也不再有'this'?
我试着浏览http://example.org/this/folder/isnt/real/whatever.php。
我希望被重定向到“http://example.org/whatever.php“与$_GET 'folder'] ='this','folder','isnt','real']相反,我被重定向到“http://example.org/browver.php”与$_GET 'folder'] ='this','this','this','this']
1条答案
按热度按时间klr1opcd1#
好了,结果是当你使用[N]循环回到一个更新的请求的开始时,%{REQUEST_URI}实际上并没有得到更新,所以对%{REQUEST_URI}的任何匹配都不会考虑对请求所做的更改。如果有任何方法可以对使用RewriteRule修改的请求执行匹配,我不知道它们。
最后,我不得不将整个URI放入查询字符串中,并从不同的方法对信息进行操作。