I am building a file system for my images on my website and I would like to have a folder tree for public images and a private folder tree for private images. My system already creates tiny urls for the images using the following .htaccess rules:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.)(..)(..)(.*)$ ./pictree/head/$1/$2/$3/$1$2$3$4.png
so this request:
www.mysite.com/1234567
would go to:
htdocs/pictree/head/1/23/45/12345657.png
I would like to modify the folder structure to include public and private folders:
htdocs/pictree/public/1/23/45/12345657.png
htdocs/pictree/private/1/23/45/12345657.png
and if a request comes from an ip address on our local network, .htaccess would attempt to access the file within the private file tree, and then the public file tree.
All other requests would only access the public tree.
I'm guessing the new rule would like like this:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REMOTE_ADDR} ^###\.###\.##
RewriteRule ^(.)(..)(..)(.*)$ ./pictree/private/$1/$2/$3/$1$2$3$4.png
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.)(..)(..)(.*)$ ./pictree/public/$1/$2/$3/$1$2$3$4.png
How do I finish it up?
You should use N flag to re-run rewriting process after matched first rewrite rule:
#check private image
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REMOTE_ADDR} ^###\.###\.##
RewriteRule ^(.)(..)(..)(.*)$ ./pictree/private/$1/$2/$3/$1$2$3$4.png [N]
#if private image not exist and we are requesting from internal network try public image
#these rewrite rules are dedicated for sub-request
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REMOTE_ADDR} ^###\.###\.##
RewriteRule ^pictree/private/(.*)$ ./pictree/public/$1
#in other cases get public image
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.)(..)(..)(.*)$ ./pictree/public/$1/$2/$3/$1$2$3$4.png