Rewrite local image path to remote path

You, DrupalHtaccess

When a client already have a Website and they needed to do some improvements to the existing site, it is important to get the same copy (as close as possible) from their production and set it up local. But how about files?. Can we download all their files? how big would those be?. So it might be a good idea to reference remote images from local. To do that you need to add following rewrite rule to the vhost.

The following is for Drupal 7 installation and replace the REMOTE_URL with your production URL.

RewriteEngine on

# Force image styles that have local files that exist to be generated.
RewriteCond %{REQUEST\_URI} ^/sites/(\[^\\/\]\*)/files/styles/\[^\\/\]\*/public/((.\*))$
RewriteCond %{DOCUMENT\_ROOT}/sites/%1/files/%2 -f
RewriteRule ^(.\*)$ $1 \[QSA,L\]
# Otherwise, send anything else that's in the files directory to the
# production server.
RewriteCond %{REQUEST\_URI} ^/sites/\[^\\/\]\*/files/.\*$
RewriteCond %{REQUEST\_URI} !^/sites/\[^\\/\]\*/files/css/.\*$
RewriteCond %{REQUEST\_URI} !^/sites/\[^\\/\]\*/files/js/.\*$
RewriteCond %{REQUEST\_FILENAME} !-f
RewriteCond %{REQUEST\_FILENAME} !-d
RewriteRule ^(.\*)$ REMOTE\_URL/$1 \[QSA,L\]
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.