CDN 相关:从 .htaccess 自动重写图像 URL [英] CDN Related : Rewrite Image URLs automatically from .htaccess

查看:24
本文介绍了CDN 相关:从 .htaccess 自动重写图像 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我买了一个 CDN 并进行了设置.在我的站点中,图像存储在名为图像"的文件夹中,并且图像 url 显然是以这种方式链接的.(*例如:images/some_image.png*)

Recently I bought a CDN and set it up. In my site, the images are stored in a folder named 'images' and the Image urls are obviously linked in this manner. (*Ex : images/some_image.png*)

因为我想以正确的方式使用 CDN,所以我需要重写 url,而不必手动更改每个图像路径.

Since I want to use the CDN right way, I need to rewrite the urls without having to manually change each and every image path.

我尝试了一个 .htaccess 代码,它被建议用于类似的问题

I tried an .htaccess code which was suggested for a similar problem

RewriteEngine On
RewriteBase /
RewriteRule ^images/(.*)$ http://cdn.mydomain.com/$1 [L,R=301]

但这似乎无法正常工作,因为所有图像都没有正确链接.

But that didn't seem to work properly as all the images were linked improperly.

所以我想知道这段代码的变化.任何回应将不胜感激.

So I would like to know the changes in this code. Any response would be appreciated.

推荐答案

这应该有效:

RewriteCond %{HTTP_HOST} ^yourdomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com$
RewriteRule ^images/?(.*)$ "http://cdn.yourdomain.com/$1" [R=301,L]

但是,请注意,这只是一个临时解决方案!
为了最大限度地利用 CDN,您需要手动将图像指向 CDN,以便为每个图像保存一个 HTTP.

However, please note that this is just a temporary solution!
In order to get the max out of your CDN, you need manually point images to your CDN in order to save one HTTP for every image.

这篇关于CDN 相关:从 .htaccess 自动重写图像 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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