Apache mod_重写规则保持循环

Apache mod_重写规则保持循环,apache,mod-rewrite,url-rewriting,apache2,Apache,Mod Rewrite,Url Rewriting,Apache2,我正在为mod_重写做一个非常简单的规则,但是我得到了一个循环 规则如下: 首先,如果我有这样的要求 index.php/SOMETHING.(txt,jpg,php) 我需要首先检查/SOMETHING.(txt,jpg,png)是否存在并显示它 如果文件不是index.php/SOMETING,并且是存在的真实路径,则显示该文件 如果没有,请将其传递到index.php/SOMETHING.(txt,jpg,php)并显示index.php 如果我有一个不存在的txt、jpg、php,那么除

我正在为mod_重写做一个非常简单的规则,但是我得到了一个循环 规则如下:

首先,如果我有这样的要求

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]

%{REQUEST\u URI}
变量始终以
/
开头,您使用
^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]

%{REQUEST\u URI}
变量始终以
/
开头,您使用
^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]

哇,是的,它可以工作:)如果文件不存在,我该怎么做才能真正重定向到index.php/$1,就像转到了一样,并且应该保持原样。@PartySoft这就是它应该做的,第一条规则将它重定向到
/robots.txt
,如果存在的话,第二条规则永远不会应用。如果robots1.txt存在,它会显示robots.txt,但如果robots1.txt不存在,它会在后台转到index.php,但url将是domain.com/robots1.txt,它不会保持为index.php/robots1。txt@PartySoftURL将保持
/robots1.txt
,但请求被路由到
index.php
silentlywow,是的,它可以工作:)如果文件不存在,我该怎么做才能真正重定向到index.php/$1,就像这样,它应该保持原样。@PartySoft这就是它应该做的,第一条规则将它重定向到
/robots.txt
,如果存在,第二条规则永远不会被应用。如果存在,它确实会显示robots.txt,但是如果robots1.txt不存在,它会在后台转到index.php,但url将是domain.com/robots1.txt,它不会保持为index.php/robots1。txt@PartySoftURL将保持
/robots1.txt
,但请求将以静默方式路由到
index.php