Ryan Burnette

Redirect to Production Assets When in the WordPress Development Environment

When I work on WordPress sites, I have a local development environment in Vagrant.

I import a current copy of the production database in order to work from the latest version of the site, but in some cases migrating the assets would amount to several gigabytes of data, and a lot of lost time.

I want the site to work, but migrating those assets would be a pain.

Redirect

The solution I like to employ is a redirect to the production assets.

Caddy

localhost:300 {
  @missing {
    path /wp-content/uploads/*
    not file
  }
  route {
    redir @missing http://[production-origin]{uri}
    php_fastcgi unix//var/run/php/php-fpm.sock
    file_server
  }
}

Nginx

location ~ ^\/wp-content\/uploads\/(.*)$ {
  try_files $uri @missing;
}
location @missing {
  rewrite "^/wp-content/uploads/(.*)$" "https://[production-origin]/wp-content/uploads/$1" redirect;
}

Apache

RewriteCond %{REQUEST_URI} \.(jpg|jpeg|gif|png|ico)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule wp-content/uploads/(.*) https://[production-origin]/wp-content/uploads/$1 [L]