Fetch media from production to local environment

Posted in Guides, Snippets

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 webserver files? Install this plugin!

Uploads by Proxy