PartySoft
PartySoft

Reputation: 2839

mod_rewrite rule keeps in loop

I am doing a quite simple rule for mod_rewrite yet I get a loop Here are the rules:

first if I have a request like

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

I need to first check if /SOMETHING.(txt,jpg,png) exists and display it

if the file is not an index.php/SOMETING and is a real path that exists display it

if not...pass it on to the index.php/SOMETHING.(txt,jpg,php) and show index.php

It all works but the last rule if I have a unexistent txt,jpg,php

example : http://domain.com/index.php/robots1.txt

File does not exist: /Applications/XAMPP/xamppfiles/htdocs/testredir/robots1.txt

works for any other extension...

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]

Upvotes: 1

Views: 119

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

The %{REQUEST_URI} variable always starts with a /, and you're matching against it using ^index.php/.+$, so that condition will always be false.

It looks like you want something like this:

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]

Upvotes: 1

Related Questions