Fetch media from production to local environment

Robin Nilsson • August 3, 2018


Why download images and other heavy resources to your local computer, when you have them available to the public? This does not only save space on your local hard drive. It saves a lot of time syncing resources from production to your local environment.

This snippet first checks if the resource is present in your local space. Its not? Then fetch it from the specified production URL. So simple!

For Apache

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule wp-content/uploads/(.*) \
  http://example.com/wp-content/uploads/$1 [NC,L]

For Nginx

location ~* ^.+\.(svg|svgz|jpg|jpeg|gif|png|ico|bmp)$ {
    try_files $uri @image_fallback;

location @image_fallback {
    proxy_pass http://{PROD};

Don't want to mess with the web server? Running WordPress? This plugin will do the trick! https://wordpress.org/plugins/uploads-by-proxy/