views:

35

answers:

2

Hello,

I am trying to write some mod_rewrite rules to generate thumbnails on the fly. So when this url

example.com/media/myphoto.jpg?width=100&height=100

the script should rewrite it to

example.com/media/myphoto-100x100.jpg

and if the file exists on the disk it gets served by Apache and if it doesn't exist it is called a script to generate the file.

I wrote this

RewriteCond %{QUERY_STRING}                              ^width=(\d+)&height=(\d+)
RewriteRule ^media/([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9]+)$    media/$1-%1x%2.$2   [L]
RewriteCond %{QUERY_STRING}                              ^(.+)?
RewriteRule ^media/([a-zA-Z0-9_\-\._]+)$                 media/index.php?file=$1&%1 [L]

and I get infinite internal redirects. The first condition is matched and the rule is executed and right after that I get an internal redirect.

I need advice to finish this script.

Thank you.

A: 

Try using this for your second condition:

RewriteCond %{REQUEST_FILENAME} !-f

'-f' (is regular file) Treats the TestString as a pathname and tests if it exists and is a regular file.

RewriteCond

jasonbar
A: 

Try these rules:

RewriteCond %{QUERY_STRING} ^width=(\d+)&height=(\d+)$
RewriteCond %{DOCUMENT_ROOT}/media/$1-%1x%2.$2 -f
RewriteRule ^media/([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9]+)$ media/$1-%1x%2.$2 [L]
RewriteCond $1 !=index.php
RewriteRule ^media/([a-zA-Z0-9_\-]+\.[a-zA-Z0-9]+)$ media/index.php?file=$1 [L,QSA]

The second condition of the first rule tests if there is a file with that name (-f).

Gumbo
Virgil