1

我正在为 mod_rewrite 做一个非常简单的规则,但我得到一个循环以下是规则:

首先,如果我有类似的要求

index.php/SOMETHING.(txt,jpg,php)

我需要先检查是否/SOMETHING.(txt,jpg,png)存在并显示它

如果文件不是index.php/SOMETING并且是存在的真实路径,则显示它

如果不是...将其传递给index.php/SOMETHING.(txt,jpg,php)并显示 index.php

如果我有一个不存在的txt,jpg,php,这一切都有效,但最后一条规则

例子 :http://domain.com/index.php/robots1.txt

文件不存在:/Applications/XAMPP/xamppfiles/htdocs/testredir/robots1.txt

适用于任何其他扩展...

RewriteEngine On
RewriteBase   /


RewriteCond %{REQUEST_URI}  ^index.php/.+$
RewriteRule ^index.php/(.+)\.jpg$ $1.jpg [NC,L]
RewriteRule ^index.php/(.+)\.txt$ $1.txt [NC,L]
RewriteRule ^index.php/(.+)\.php$ $1.php [NC,L]


#here I am missing a rule but if i put the following it will loop

#RewriteCond %{REQUEST_URI} -f
#RewriteRule .*  index.php/$0 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^index.php/.+$
RewriteRule .* index.php/$0 [PT]
4

1 回答 1

1

%{REQUEST_URI}变量始终以 a 开头/,并且您正在使用 匹配它^index.php/.+$,因此该条件将始终为 false

看起来你想要这样的东西:

RewriteEngine On
RewriteBase   /

RewriteCond %{THE_REQUEST} \ /index\.php/(.+)\.(jpg|txt|php)
RewriteRule ^ /%1.%2 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L,PT]
于 2013-11-13T02:36:38.267 回答