[英]RewriteRule acting strange
我想要这样的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
脚本中得到folder='images'
和picture='pic1.jpg'
发生这种情况是因为您的重写规则正在循环并使目标字符串subdir/index.php
与.*/.*
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.