CDN: Rewrite image URL automatically from .htaccess

I recently bought a CDN and configured it. On my site, the images are stored in a folder called β€œimages”, and the image URLs are obviously related in this way. (* Ex: images / some_image.png *)

Since I want to use the CDN correctly, I need to rewrite the URLs without manually changing each image path.

I tried the .htaccess code that was suggested for a similar problem

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

But this does not work properly, since all the images were connected incorrectly.

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

+4
source share
1 answer

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!
To get the most out of your CDN, you need to manually map the points to the CDN to save one HTTP for each image.

+9
source

Source: https://habr.com/ru/post/1383286/


All Articles