Development and staging servers may have less disk space allocated to them than a production server. When you are copying files from your production server back to staging or development, you can quickly fill up your disk space. If this is an issue, it's possible to use the files on the production server for your staging site without copying them. Two of the easier methods you can use are: the Stage File Proxy module or an .htaccess
redirect.
Stage File Proxy
Using the Stage File Proxy module enables a development server to maintain a clean files directory and use files from an alternate source by directly referencing that alternate source. An additional option within the module allows the file system on the development server to be seeded with files from the production server.
.htaccess
rewrites
If you prefer a lower impact solution and are willing to work in the .htaccess
file, you can use rewrite rules to intercept calls to the file system on development or stage that would otherwise result in an HTTP 404 error.
This procedure assumes that you are placing your files in the sites/default/files
directory, that the request is going to sites/default/files
, and that the system this code is on is non-production.
This enables a user on a non-production site to view any image that has been either uploaded or generated on the production server. Be aware that if the file exists on the non-production server, this redirect may not work properly.
- Edit your website's
.htaccess
file and look for the following lines:# Pass all requests not referring directly to files in the file system to # index.php. Clean URLs are handled in drupal_environment_initialize().
- Depending on your environment, paste the appropriate code block before the previous lines of code:
- Acquia Cloud users:
# If the request is to the files directory. RewriteCond %{REQUEST_URI} ^/sites/default/files/(.*)$ # and the environment is not production RewriteCond %{ENV:AH_SITE_ENVIRONMENT} !^prod$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Redirect the request to the production server. RewriteRule ^sites/default/files/(.*)$ http://example.com/sites/default/files/$1 [L]
- Non-Acquia Cloud users:
If you aren't an Acquia Cloud user, you can modify the following code to make it useable in your environment, where example.com is the domain that you're redirecting to:# SetEnvIf must be used for a variable to be available # a RewriteCond in mod_rewrite. SetEnv is not sufficient. SetEnvIf Host . AH_SITE_ENVIRONMENT=prod # If the request is to the files directory. RewriteCond %{REQUEST_URI} ^/sites/default/files/(.*)$ # and the environment is not production RewriteCond %{HTTP_HOST} !^(www.)?example.com$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Redirect the request to the production server. RewriteRule ^sites/default/files/(.*)$ http://example.com/sites/default/files/$1 [L]
- Acquia Cloud users: