Apache2 rewrite rule to show remote images on your local machine

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:33

With the following directive in your Apache2 virtual host file (at /etc/apache2/sites-available) you must not download the images from products etc. on your local system for development. You just map your local folder location to the remote one. The following example is for a Drupal site (files are under /sites/default/files):

RewriteEngine On
RewriteOptions Inherit
RewriteRule /sites/default/files/(.*)\.(jpeg|jpg|gif|png)$ http://www.my-website.com/sites/default/files/$1.$2 [L,R=301]


This 1 line does also work:

RedirectMatch ^/media/(.*)\.(jpeg|jpg|gif|png)$ https://www.my-website.com/static/media/$1.$2

This setting can be also made in the .htaccess file in your project folder. It's important that this setting is made in theĀ <IfModule mod_rewrite.c> section.