RewriteRule表现得很奇怪

时间:2015-05-29 17:16:21

标签: regex apache .htaccess mod-rewrite

我想要这样的网址:localhost/dir/subdir/index.php?folder=images&picture=pic1.jpg

重写为:localhost/dir/

所以我在RewriteEngine On RewriteRule ^(.*)/(.*)$ subdir/index.php?folder=$1&picture=$2 [L] 中添加了非常简单的.htaccess文件:

folder='images'

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

奇怪的是,当我修改.htaccess文件以从同一目录(而不是'subdir')调用index.php时,它运行良好:

folder='images'

我在picture='pic1.jpg'脚本

中获得localhost/dir/index.php和{{1}}

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您的重写规则正在循环并将目标字符串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]