jnemecz
jnemecz

Reputation: 3608

Apache mod_rewrite rule and forever loop

I need to rewrite all files that start with "t" from one directory to another directory.

From /gallery/tXYZ.ext to /gallery/thumbnails/thumbs_XYZ.ext

I have problem with this RewriteRule:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^gallery/t(.*)$  /gallery/thumbs/thumbs_$1 [R=301,NC,L]

because it catches thumbs directory again (starts with "t") and forever loop occurs.

Upvotes: 1

Views: 31

Answers (1)

anubhava
anubhava

Reputation: 785146

Problem is the word thumbs also matches t* pattern and caused redirection loop.

Use this rule to fix it:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} !^/gallery/thumbs/ [NC]
RewriteRule ^gallery/t(.*)$ /gallery/thumbs/thumbs_$1 [R=301,NC,L]

OR else:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^gallery/t(?!humbs/)(.*)$ /gallery/thumbs/thumbs_$1 [R=301,NC,L]

Upvotes: 1

Related Questions