[development] Rewrite rules and file access

Jamie Holly hovercrafter at earthlink.net
Wed Jul 14 13:15:04 UTC 2010


The best trick is to not use regular Drupal file paths on your pages and 
instead use an internal Drupal path, the same way the private download 
method does. Just alter your links using hook_node_view.

If you want to keep the paths intact, then it takes a couple of 
.htaccess tricks.

First to answer your question, yes Drupal's .htaccess rules check if a 
file exists and if so it serves it directly. This is necessary for 
things like css, js and theme images to load properly.

You can force .htaccess to ignore certain paths. In Drupal's .htaccess, 
right before Drupal's rewrite rules (right after # RewriteBase /):

RewriteCond %{REQUEST_URI} /sites/default/files/imagecache/.*
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Now any file request to /sites/default/files/imagecache/* will go 
through Drupal. Everything else will be handled normally.

One note of caution though. If you are using this for thumbnails and you 
have a page with say 20 thumbnails on it, it will be a major performance 
hit. You are requiring Drupal to bootstrap for every one of those 
requests, so if a person hits that page, Drupal has to fire up 21 times 
(20 for images, once for the page). Even a decent dedicated server will 
have problems with that.

The better alternatives are to either watermark all your thumbnails (ie: 
ImageCache Actions) and serve them directly, or to use a CDN that offers 
token based authentication, like VoxCAST.

Jamie Holly
http://www.intoxination.net
http://www.hollyit.net


On 7/14/2010 5:58 AM, Lee Rowlands wrote:
> Hi
> I asked this one on the list during Drupalcon SF but got no response
> (the list was quiet that week!) so here goes again:
>
> *The Background*
> ---------------
> I've got a module I'm working on that needs to force some images to go
> via the private download method.
> This is working fine rerouting links via system/files in combination
> with a htaccess file limiting direct access to the directory.
> I also need thumbnails in various sizes of the images so it makes sense
> to use imagecache.
> I want the site in public download method except for these files and
> I've successfully got the imagecache integration working.
> I've used hook_menu_alter to prevent people accessing the relevant
> imagecache file path directly, rerouting the imagecache cached images
> via system/files.
> The problem is, once the imagecache file is created, the file is no
> longer being served via Drupal, with Apache serving it direct. This
> obviously does not get near my access callback.
>
> *The Questions*
> ---------------
> Is this down to Drupal's rewrite rules?
> Are the rewrite rules not evaluated if the file actually exists?
> Can I create a rewrite rule to force this file to go through Drupal -
> the relevant files all contain a common element in their path so I
> should be able to match it) or do I need to consider alternatives such
> as creating .htaccess files in the subfolders for each imagecache
> preset, monitoring for new presets?
>
> Any suggestions welcome.
> Lee Rowlands
>
>
>
>    


More information about the development mailing list