Modify .htaccess And Use The Media Directory Of Another Site (or other folders)

Submitted by deeperdata - 2 years ago

Often when I setup a staging/development environment, I only migrate the minimum codebase (usually the Git repo) to the testing server. I don't include the media/ folder out of habit primarily because seeing product images and other image placeholders isn't necessary when focusing on layout and functionality. However, when it comes time to demo your design to a respective client, they may not appreciate your effective approach to minimizing load times and reserving bandwidth. On that occasion, I suggest linking the contents of your media/ directory to the the production server using an .htaccess rewrite. This will seamlessly allow your test site to load images from the real site without so much as a hiccup. There are certain drawbacks to this approach. It assumes you won't be uploading new product or WYSIWYG images anytime soon. If what you are demoing/showcasing requires a lot of that then you may just want to import the contents of the media directory. However, if you are testing extensions, trying out new features, optimizing the site or anything else unrelated to the catalog or CMS pages, this trick could very well be one your favorites.

// Modify the .htaccess file located in your webroot and add the following
// anywhere after RewriteEngine On. Replace "production-site.com" with
// your website. You can also replace "media" with any directory you
// wish to emulate

############################################
## Media Directory Rewrites
RedirectMatch 302 ^/media/(.*)$ http://production-site.com/media/$1



// This can also be done from within the media directory itself by
// adding the following lines to media/.htaccess

RewriteEngine On
RewriteRule ^(.*)$ http://production-site.com/media/$1 [R=302]
affiliate_link
Learn PHP
comments powered by Disqus
Proudly hosted on Digital Ocean