Regex 我的行为很奇怪

Regex 我的行为很奇怪,regex,apache,.htaccess,mod-rewrite,Regex,Apache,.htaccess,Mod Rewrite,我想要这样的URL:localhost/dir/images/pic1.jpg 要重写为:localhost/dir/subdir/index.php?folder=images&picture=pic1.jpg 所以我把真正简单的.htaccess文件放在localhost/dir/中: RewriteEngine On RewriteRule ^(.*)/(.*)$ subdir/index.php?folder=$1&picture=$2 [L] 希望在localhost/dir

我想要这样的URL:
localhost/dir/images/pic1.jpg

要重写为:
localhost/dir/subdir/index.php?folder=images&picture=pic1.jpg

所以我把真正简单的.htaccess文件放在
localhost/dir/
中:

RewriteEngine On
RewriteRule ^(.*)/(.*)$ subdir/index.php?folder=$1&picture=$2 [L]
希望在
localhost/dir/subdir/index.php中获得
folder='images'
picture='pic1.jpg'
,但是我有
folder='subdir'
picture='index.php'

奇怪的是,当我修改.htaccess文件以从同一目录(而不是从“subdir”)调用index.php时,它工作得很好:

RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?folder=$1&picture=$2 [L]

我在
localhost/dir/index.php
script

中得到了
folder='images'
picture='pic1.jpg'
,这是因为重写规则正在循环并将目标字符串
subdir/index.php
与模式
*/.
匹配

使用此条件停止循环:

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 ^(.*)/(.*)$ subdir/index.php?folder=$1&picture=$2 [L]

我没有意识到单一规则可能会被多次调用。我读了一些教程,其中没有一个提到这一点,也没有人警告过循环的可能性。他们应该用大红字写上;)谢谢!很高兴它对你有用。是的,大多数教程都关注语义,但没有解释整个单词是如何流动的。