CDN相关:重写图片来源的.htaccess自动网址重写、来源、网址、图片

2023-09-02 00:21:51 作者:春风乍起

最近我买了一个CDN,并设置它。在我的网站,影像储存在名为照片文件夹中的图片网址以这种方式显然是联系在一起的。 (*例如:图像/ 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 code这是提出了类似的问题。

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.

所以,我想知道的变化,这code。任何反应将是AP preciated。

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,你需要手动为救一个HTTP,每一张图像指向CDN。

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.