mod_rewrite规则保持循环

时间:2013-11-13 02:20:45

标签: apache mod-rewrite url-rewriting apache2

我正在为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]

1 个答案:

答案 0 :(得分:1)

%{REQUEST_URI}变量始终以/开头,并且您使用^index.php/.+$与之匹配,因此条件将始终为false 。< / p>

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

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]