Apache - retrieve file from S3 if it does not exist on disk (Misc)

On hosted systems with limited disk space, you might occasionally want to archive files out to Amazon S3 (or some other filer). This would usually result in URLs changing, though, so the ideal situation would be to have Apache simply proxy the request through to your filer if it cannot find the file on disk

This snippet checks for the file within the document root and if it does not exist, sends a request our to an S3 bucket

Details

  • Language: Misc

Snippet

SSLProxyEngine On

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ https://s3.[amazonregion].amazonaws.com/[yourbucketname]/$1 [P,QSA,L]

Usage Example

SSLProxyEngine On

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ https://s3.eu-west-3.amazonaws.com/examplebucket/$1 [P,QSA,L]