Fetch Media from Production or Staging Server

For local development and testing you do not have to copy all media files to your local environment.

The following redirect or proxy directives will load all media and thumbnail files not available locally from a staging or production server.

When using apache (webserver-type: apache-fpm) add these lines at the beginning of your public/.htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/(media|thumbnail)/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ https://staging.example.com/$1 [L,R=301]
</IfModule>

Do not commit this updated public/.htaccess file to your repo.

When using nginx (webserver-type: nginx-fpm), create a file .ddev/nginx/media-redirect.conf with the following contents (with optional basic auth):

    location @mediaserver {
        proxy_pass https://staging.example.com;
        proxy_set_header Authorization "Basic your-basic-auth-secret";
    }

    location ^~ /media/ {
        access_log off;
        expires max;
        try_files $uri $uri/ @mediaserver;
        break;
    }

    location ^~ /thumbnail/ {
        access_log off;
        expires max;
        try_files $uri $uri/ @mediaserver;
        break;
    }

The nginx way obviously has the advantage that it can be committed to your repository and, once setup, doesn't need any further attention.

Also see https://notebook.vanwittlaer.de/hosting/media-urls-out-of-sync-between-servers.

Last updated

Was this helpful?