Virtual Host Rewriting in MAMP

While developing Drupal websites locally, I found it to be somewhat of a pain to sync all of the uploaded files (specifically images) from the server to my computer. In some cases, this could be hundreds of megabytes worth of files to download, but without downloading them, my local version of the site would look incomplete with missing images.

I primarily use MAMP for local development, and it turns out that MAMP PRO has an option to add additional virtual host parameters to each individual site hosted locally within the app. Using this feature, I am able to add a rewrite rule to have the MAMP look for photos on the live site when they don’t exist locally, removing the need to download the files onto my computer.

Here’s how to access the VirtualHost section in MAMP PRO:

  1. Select your server under the “Hosts” tab in MAMP PRO.
  2. Click “Extended” on the right in the host settings.
  3. Add your custom code in the box in the “Additional parameters for <VirtualHost>” section.

And here is the code I use that will look for uploaded files on a live Drupal site if they do not exist locally:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/sites/default/files/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/sites/default/files/(.*)$$1 [L]

Just change the domain and directories to reflect your file locations, and you’re good to go.