Recently I installed the CDN module. To avoid duplicate content I added a RewriteRule to my Apache config:

RewriteCond %{REQUEST_URI} !(^/(.*)(.js|.css|.jpg|.jpeg|.gif|.png)) [NC]
RewriteCond %{HTTP_ACCEPT} !(.*image.*|.*css.*|.*javascript.*) [NC]
RewriteCond %{HTTP_HOST} ^(static1.domain.com|static2.domain.com)$ [NC]
RewriteRule ^(.*)$ http://domain.com$1 [L,R=301]

This is working, except when I'm using image styles. If the image exists everything works, but when it doesn't exist, I get a 301 to http://domain.com/index.php

These are the current situations using the implemented RewriteRule:

  • GET static1.domain.com - 301 to http://domain.com - OK
  • GET static1.domain.com/image.jpg - 200 - OK
  • GET static1.domain.com/sites/domain/files/styles/.../image.jpg when file exists - 200 - OK
  • GET static1.domain.com/sites/domain/files/styles/.../image.jpg when file doesn't exist - 301 to http://domain.com/index.php - WRONG

How do I force the image style file to be created when browsing to it from a static URL?

2 Answers 2


I dont really understand your question, but we handled a similar problem. We also did rewrtie for all images to static server (different server running lighttpd). On the lighttpd the files folder was mounted as well, so all files are in sync.

We also had a domain that forced back to one of the webservers (varnish>drupal), and not to use the CDN server . If the file did not exist in the static server it got rewrtitten (temporary) back to the webserver, which created the imagecache file. From that moment, the file was there and it would always be there.

attr = lighty.stat(lighty.env['physical.path'])
 if (not attr) then
 lighty.header["Location"] = "http://directto.webserver.com"  .. lighty.env["request.uri"]
 return 302

Hope this can help.

  • This seems to be a similar problem indeed. What I need is a solution like the one you provided (+1) but for Apache (hence my RewriteRule).
    – Bart
    Apr 4, 2012 at 14:25

Apache directives are context sensitive. Since the RewriteRule was in a VirtualHost, the directives were different than normal.

This is the working version:

RewriteCond %{HTTP_HOST} ^(static1.domain.com|static2.domain.com)$ [NC]
RewriteRule ^(.*)$ http://domain.com$1 [L,R=301]

The RewriteRule behaves different than in the original post, it just checks for files instead of types of files, it behaves like described in drupal_sensei's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.