1

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?

nev
  • 133
  • 3
  • 10
  • This seems to be just a typo? `RewriteCond %{HTTP_HOST} $www\.` is: "If the variable `%{HTTP_HOST}` has the literal string `www.` after the end of the string", which is obviously impossible. You likely wanted to use the begin-of-string-character `^`. – Sumurai8 Jun 16 '15 at 14:54
  • So you would recast that as - what? – nev Jun 16 '15 at 15:03
  • `RewriteCond %{HTTP_HOST} ^www\.` – Sumurai8 Jun 16 '15 at 15:27
  • Well yes that's what I thought you meant, but still, strangely, it's not working.. – nev Jun 16 '15 at 15:34
  • Please check that .htaccess files are read in the first place (enter garbage in .htaccess, reload page. If you get a 500 error, it is read). Please check that mod_rewrite is enabled on your server. (see [here for example](http://stackoverflow.com/questions/9021425/how-to-check-if-mod-rewrite-is-enabled-in-php)). A restart of Apache is required if you have just enabled it. Make sure that you set AllowOverride to at least FileInfo in your httpd.conf ([docs](http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride)). If you have access to httpd.conf, I would suggest putting the rules there. – Sumurai8 Jun 16 '15 at 15:48
  • Please note that changes in httpd.conf are not reflected until Apache is restart (which is also the strength of putting things there). It save Apache a lot of file reading. – Sumurai8 Jun 16 '15 at 15:49
  • The garbage read check was OK, and I have mod_rewrite. The other I can't do because it's on a shared server, but it should be working anyway. I tried commenting out everything preceding the code in the .htaccess file but still no good, so I'm stumped. – nev Jun 16 '15 at 16:50
  • Is your .htaccess file in your document root? I can't test your rule here, but it seems correct to me. Escaping the slashes is not necessary, but not incorrect afaik. What happens if you use the rule `RewriteRule ^foo /bar [R,L]` and go to `www.example.com/foo`? – Sumurai8 Jun 16 '15 at 17:59
  • Yes .htaccess is in root, and foo goes to bar as you'd expect. Being told off here for extended discussion so this remains an unknown to me. – nev Jun 16 '15 at 23:40
  • Testing this at home it works as you would expect, except for the fact that `R=perm` will be treated as a temporary redirect. The correct named ones are `R=temp`, `R=permanent` and `R=seeother`. It is sometimes just easier to use the status code (e.g. `R=301`). The only thing I can think of is that you have other rules that conflict with this one, either in this .htaccess or in a .htaccess file in one of the subdirectories. – Sumurai8 Jun 17 '15 at 09:58

1 Answers1

0

Try the below:

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