Forum Moderators: phranque

Message Too Old, No Replies

[Regex] Redirection loop

Can't get out of this redirection loop, pb with my regex

         

Peutch

2:10 pm on Nov 28, 2004 (gmt 0)

10+ Year Member



Hi every one,
I would like to make a redirection. Basically, I want my JPG files to be opened via a PHP script, so I can track stats for these files.
Let's say that someone wants to open the photo /photos10-19/photos16/example.jpg

I want it to be redirected to : openfile.php?group=photos10-19&dir=photos16&file=example.jpg

Here is the rewrite rule I use in my htaccess but it gets into a loop :


#RewriteRule ^photos([0-9-]+)/photos([0-9]+)/(.*([^_tmb]))\.(jpg¦JPG)$ /openfile.php?group=photos$1&dir=photos$2&file=$3.$5

You will notice that I do not want this redirection to apply to files whose filename ends with _tmb (e.g. not apply to example_tmb.jpg) but this is not the most important thing.

So basically, I don't want the redirection to apply if the file starts with openfile.php?

Would you know how I can do that?

Thank you much!
Peutch

jdMorgan

4:20 pm on Nov 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Peutch,

Welcome to WebmasterWorld!

I see no reason why your code would loop. Your pattern starts with "/photos", and will therefore not match "/openfile.php". So, there is no possibility of a single-rewrite loop. (What I mean by that is that if you rewrite from A to B, then B does not match A and the rule won't loop. There is the possibility that you might have two rules, one that rewrites from A to B and another that rewrites from B to A, and then you can get a two-rule loop.)

Lacking any further information on why you think this is looping, about the only useful thing I can do is to point out that "[^_tmb]" means, 'match a single character, not equal to an undescore, "t", "m" or "b".' It will not exclude the string "_tmb" as you expect. However, it will approximate the desired function, because any URL that contains an underscore in that position will not match the rule, so the rule won't be invoked.

A better way to do complex exclusion is to use RewriteCond:
RewriteCond %{REQUEST_URI} !^/photos([0-9-]+/photos[0-9]+/.+_tmb
RewriteRule ^photos([0-9-]+)/photos([0-9]+)/(.+\.jpg)$ /openfile.php?group=photos$1&dir=photos$2&file=$3 [NC,L]

Adding [NC] makes the pattern match case-insensitive, and [L] will stop further processing of rewriterules if the current rule matches and is invoked. Rearranging the parentheses and eliminating the nested ones further simplifies the rule.

Jim