CDN Related : Rewrite Image URLs automatically fro

2020-02-06 03:50发布

问题:

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*)

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.

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.

回答1:

This should work:

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

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.