When developing web shops (using Magento 2, Shopware 6, ...), we do not want to copy all media assets to the local development machine.
Still it would be nice, to get a real view on the system, i.e. load the media images on demand from the production server.
How can this be achieved?
In case Shopware 6 is used, this might also be an option:
https://github.com/tinect/TinectMediaStaging
If requests the missing media files from production.
In Shopware 6, also the following rewrite rule in Apache would do.
In other Shopsystem, it depends a bit if the local installation needs to access the local images (for example to determine size / resize) or just includes the fixed URL in the HTML which then could be rewritten using a similar rule.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/media/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ https://shop.example.com/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/thumbnail/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ https://shop.example.com/$1 [QSA,L]
</IfModule>
The beauty is, that you can still upload new pictures locally for testing, because it first checks if they are available locally.
Eventually add twice
RewriteCond %{HTTP_HOST} \.(dev|local)$ [NC]
If your dev systems end in .dev
or .local
so you can use this in the generic .htaccess
without needing a modified one for the local dev.