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 WordPress is installed in a directory named "wordpress". The CDN domain is in the form

RewriteEngine On
RewriteCond %{HTTP_HOST} $www\.    
RewriteRule (wordpress\/wp-content\/uploads\/.*\.jpg)$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?


  • Martin

    Try the below:

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

Add Comment