Jump to content

Serve Images From Outside Webroot


Mahngiel

Recommended Posts

I have an application that runs several sites on base configs, but allows those sites to change their imagery. As it stands right now, the image paths and .htaccess is as so:

 

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule img/.+/(.*) img/$1 [L]

 

www/

\ js/

\ img/

\\ site1/

\\ site2/

1.png

2.png...

 

 

<img src="img/{$Site->id()}/1.png" />

 

The .htaccess rule basically permits me to path all images to a user's account id, and when not found it serves from the base img directory. Nothing too special.

 

I'd rather not have to deal with write permissions, so I've made an external repository that i'll save the images into. Basic structure is /repository/site/<site id>/<img>|<pdf>|<etc>.

 

The code is versioned through github, so I don't think symlinks would work. To combat this, here is my weak attempt:

 

# when the URI matches img/<id>/<img>, serve from repo path
RewriteCond %{REQUEST_URI} img/.+/(.*)
RewriteRule img/(.+)/(.*)$ /repository/sites/$1/$2 [L]

# if that doesn't exist, server from web path
RewriteCond /repository/sites/8/%{REQUEST_FILENAME} !-f
RewriteRule img/.+/(.*) img/$1 [L]

 

Produces logs like so:

x.x.x.x- - [30/Oct/2012:14:59:28 --0400] [xx][rid#9cfe1d8/subreq] (1) [perdir /home/a/b/c/www/] internal redirect with /index.php/repository/sites/8/img/interface_splash.png [iNTERNAL REDIRECT]
x.x.78.1 - - [30/Oct/2012:14:59:28 --0400] [xx][rid#9d3a7c8/initial] (1) [perdir /home/a/b/c/www/] internal redirect with /index.php/ [iNTERNAL REDIRECT]
x.x.78.1 - - [30/Oct/2012:14:59:28 --0400] [xx][rid#9d8bdb8/initial/redir#1] (1) [perdir /home/a/b/c/www/] pass through /home/a/b/c/www/index.php
x.x.78.1 - - [30/Oct/2012:14:59:28 --0400] [xx][rid#9d42a58/subreq] (1) [perdir /home/a/b/c/www/] internal redirect with /index.php/ [iNTERNAL REDIRECT]

 

Any idea how I can dynamically server assets from outside the webroot?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.