Redirect with htaccess for images onto another server without redirect looping

戏子无情 提交于 2019-12-12 10:01:40

问题


I currently have a host where my main site is hosted on. I have set up nginx on another server to mirror/cache files being requested if it doesn't have it already, in particular images and flv videos.

For example:

www.domain.com is my main site.

www.domain.com/video/video.flv

www.domain.com/images/1.png

I would like to ask apache to redirect it to imgserv.domain.com (imgserv.domain.com points to another server IP)

imgserv.domain.com/video/video.flv

imgserv.domain.com/images/1.png

Basically redirect everything with certain filetypes and preserving the structure of the URL, like flv etc.

I tried something but I am getting a redirect looping error. Could someone help me out?

Thank you!

This is what I have at the moment

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RedirectMatch 302 ^(.*)\.gif$ http://imgserv.domain.com/forums$1.gif
RedirectMatch 302 ^(.*)\.jpg$ http://imgserv.domain.com/forums$1.jpg
RedirectMatch 302 ^(.*)\.png$ http://imgserv.domain.com/forums$1.png

回答1:


You are mixing up two different modules: RewriteEngine and RewriteCond are from mod_rewrite while RedirectMatch is from mod_alias. They can’t work together.

Try this mod_rewrite example instead:

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*\.(gif|jpg|png)$ http://imgserv.example.com/forums/$0 [L,R]


来源:https://stackoverflow.com/questions/3050935/redirect-with-htaccess-for-images-onto-another-server-without-redirect-looping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!