mod_rewrite and image redirecting
I have a website with a lot of images. Let's say these images stored in a folder "/data/images". Also I have a folder with watermarked images: "/data/wimages". Filenames of origianal and watermarked images are the same, but not all images have their watermarked copy. So, I want to redirect original images (without changing its URL in a browser or html source code) if watermarked copy presents and passing throught the original image开发者_StackOverflow社区 if not. I'm 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'm recovering previously changed url and get an infinite loop!
Any ideas?
P.S. Sorry for my English.
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]
精彩评论