0

I'm using a CDN for serving my images on my Wordpress..

The URL structure goes as such:

//mydomain.com/assets/uploads/[year]/[month]/[day]/[imagename].png or whatever

For my CDN the images are served as:

//cdn.cdnnetwork.com/mydomain.com/assets/uploads/[year]/[month]/[day]/[imagename].png or whatever

Now the problem is that whenever the image gets accessed directly either with typing in the exact url or a search engine redirecting it they all go to:

//mydomain.com/assets/uploads/[year]/[month]/[day]/[imagename].png or whatever

I want to use .htaccess rule or any other way to redirect all

//mydomain.com/assets/uploads/[year]/[month]/[day]/[imagename].png or whatever

links to:

//cdn.cdnnetwork.com/mydomain.com/assets/uploads/[year]/[month]/[day]/[imagename].png or whatever

So I can save bandwidth... Is it possible? The images embedded in a actual webpage using the img tag are fine it's just direct access to the images.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
ddshd
  • 15
  • 1
  • 5

1 Answers1

0

This should be doable. Your .htaccess file (in www root) would look something like this:

RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]

RewriteCond %{HTTP_HOST} ^mydomain.com/assets$
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ %{ENV:proto}://cdn.cdnnetwork.com/mydomain.com/$1 [L,R=301]

See this answer for more information on how to preserve the protocol (http/https): Preserve HTTP/HTTPS protocol in .htaccess redirects

Community
  • 1
  • 1
markwatsonatx
  • 3,391
  • 2
  • 21
  • 19