Mod_rewrite and image redirection

I have a website with a lot of images. Say these images are stored in the "/ data / images" folder. I also have a folder with watermarks: "/ data / wimages". The source and watermark names are the same, but not all images have a watermarked copy. So, I want to redirect the source image (without changing its URL in the browser or in the HTML source), if the watermark represents and passes through the source image, if not. I am trying to solve this problem using Apache mod_rewrite:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f # Checking if file exists and is a regular file.
RewriteCond %{REQUEST_URI} ^.*/data/images/.*\.(gif|jp?g|png)$ [NC] # Is file an image?
RewriteRule ^data/images/(.*\.(gif|png|jp?g))$ /data/wimages/$1 [NC] # New watermaked image url. 
RewriteCond %{REQUEST_FILENAME} !-f # If watermarked image not exists ..
RewriteRule ^data/wimages/(.*\.(gif|png|jp?g))$ /data/images/$1 [NC,L]

then I restore the previously changed url and get an infinite loop!

Any ideas?

PS Sorry for my English.

+3
source share
1 answer
RewriteEngine on

RewriteCond %{QUERY_STRING} !rewritten
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^.*/data/images/.*\.(gif|jpe?g|png)$ [NC]
RewriteRule ^data/images/(.*\.(gif|png|jpe?g))$ /data/wimages/$1?rewritten=1 [QSA,NC]

RewriteCond %{QUERY_STRING} !^rewritten=2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^data/wimages/(.*\.(gif|png|jpe?g))$ /data/images/$1?rewritten=2 [QSA,NC,L]
+3

All Articles