Redirect image requests from locations on origin domain to CDN domain

I'm setting up a WordPress site with the CloudFront CDN, also using the W3 Total cache plugin to configure much of it.

Someone suggested that, because of the possibility of old URLs remembered by Google, or for other reasons, I should use an .htaccess rule to redirect requests for images that live inside the level of the WordPress installation of the origin domain, to the CDN domain. I guess it's just to be on the safe side to avoid apparently duplicate content.

But the rule he suggested for the root .htaccess file doesn't appear to be working. Here's what he suggested (The origin domain is in the form http://www.example.com. WordPress is installed in a directory named "wordpress". The CDN domain is in the form http://cdn.example.com):

RewriteEngine On
RewriteCond %{HTTP_HOST} $www\.    
RewriteRule (wordpress\/wp-content\/uploads\/.*\.jpg) http://cdn.example.com/$1 [L,R=perm]

I find this doesn't redirect any requests for jpg files that live in the WordPress directory.

What is wrong with it? I also would like to know how can I include other file types as well, and, how to redirect those file-types which live in other directories - the same logic on a new line?

Comments


  • Martin

    Try the below:

      RewriteCond %{HTTP_HOST} ^(www\.)?example.com
      RewriteCond %{REQUEST_URI} ^(.*)\.jpg$
      RewriteRule ^/(.*)$ http://cdn.example.com/$1 [NC,NE,R=301]
    

Add Comment