Site Files Redirect


Redirects your local environment's Drupal install /sites/*/files/* requests to a server that actually has them. This helps avoid having to sync (sometimes, very large) file structures onto your local machine and taking up valuable hard-drive space.

Get raw version
  1. ### Apache Rewrite
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine on
  4. # Force image styles that have local files that exist to be generated.
  5. RewriteCond %{REQUEST_URI} ^/sites/([^\/]*)/files/styles/[^\/]*/public/((.*))$
  6. RewriteCond %{DOCUMENT_ROOT}/sites/%1/files/%2 -f
  7. RewriteRule ^(.*)$ $1 [QSA,L]
  8. # Otherwise, send anything else that's in the files directory to the
  9. # production server.
  10. RewriteCond %{REQUEST_URI} ^/sites/[^\/]*/files/.*$
  11. RewriteCond %{REQUEST_URI} !^/sites/[^\/]*/files/css/.*$
  12. RewriteCond %{REQUEST_URI} !^/sites/[^\/]*/files/js/.*$
  13. RewriteCond %{REQUEST_FILENAME} !-f
  14. RewriteCond %{REQUEST_FILENAME} !-d
  15. RewriteRule ^(.*)$$1 [QSA,L]
  16. </IfModule>
  18. ### nginx Rewrite
  19. ## Try local files first, otherwise redirect to a development server.
  20. location ^~ /sites/default/files {
  21. try_files $uri @dev_redirect;
  22. }
  23. ## Development Server Redirect
  24. location @dev_redirect {
  25. rewrite ^$request_uri? permanent;
  26. }


mark.carver's picture

From my comment at:

>Taking a look at the code briefly, I noticed that it definitely has some advantages since it's tied into Drupal (mainly with image styles). However, it's pretty much doing the same thing as an Apache rewrite, only difference is that it's bootstrapping Drupal so the module can load. This method is attempting to redirect from the get go, bypassing Drupal entirely. Both have their advantages, I doubt it could be considered "worse" though.

benjamin_dk's picture

I can't get this piece of cod to work (=not found images are not being replaced) when I place it in my root folder in the .htaccess file after this part:

  1. # Pass all requests not referring directly to files in the filesystem to
  2. # index.php. Clean URLs are handled in drupal_environment_initialize().
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteCond %{REQUEST_URI} !=/favicon.ico
  6. RewriteRule ^ index.php [L]

Any hints? Would be so sweet if it worked...