mod_rewrite的和图像重定向 [英] mod_rewrite and image redirecting

查看:92
本文介绍了mod_rewrite的和图像重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的图片的网站。比方说,存储在文件夹/数据/图像中的这些图像。我也有水印的图像的文件夹:/数据/ wimages。 origianal和水印的图像的文件名是相同的,但不是所有的图象具有其水印复制。所以,我想重定向原始图像(无需在浏览器或HTML源$ C ​​$ c更改其URL),如果水印复制presents并通过throught原始图像如果没有。我试图解决使用Apache的mod_rewrite此问题:

  RewriteEngine叙述在
的RewriteCond%{} REQUEST_FILENAME -f#检查,如果文件存在且是一个普通文件。
。的RewriteCond%{REQUEST_URI} ^ * /数据/图像/.* \\(GIF?| JP G | PNG)。$ [NC]#是文件的图像?
重写规则^数据/影像/(* \\(GIF |。PNG?| JP G))$ /数据/ wimages / $ 1 [NC]#新watermaked图像的URL。
的RewriteCond%{} REQUEST_FILENAME!-f#如果水印图像不存在..
重写规则^数据/ wimages /(* \\(GIF |。PNG?| JP G))$ /数据/图像/ $ 1 [NC,L]

然后我恢复previously更改URL,并得到一个无限循环!

任何想法?

P.S。对不起,我的英语水平。


解决方案

  RewriteEngine叙述上的RewriteCond%{QUERY_STRING}!重写
的RewriteCond%{} REQUEST_FILENAME -f
的RewriteCond%{REQUEST_URI} ^ * /数据/图像/.* \\。(GIF?| JPE G | PNG)$ [NC]
重写规则^数据/影像/(* \\(GIF |。PNG?| JPE G))?$ /数据/ wimages / $ 1 =改写1 [QSA,NC]的RewriteCond%{QUERY_STRING}!^改写= 2
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^数据/ wimages /(* \\(GIF |。PNG?| JPE G))?$ /数据/图像/ $ 1 =改写2 [QSA,NC,L]

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 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]

这篇关于mod_rewrite的和图像重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆