Skip to content

Instantly share code, notes, and snippets.

@pogla
Last active January 24, 2019 10:01
Show Gist options
  • Save pogla/a6aa09482b75929be61499b5adeef84f to your computer and use it in GitHub Desktop.
Save pogla/a6aa09482b75929be61499b5adeef84f to your computer and use it in GitHub Desktop.
Load remote images if they don't exist locally - WordPress

NGINX

Serve all assets:

location ~ ^(/wp-content/themes|/wp-content/uploads)/.*\.(jpe?g|gif|css|png|js|ico|pdf|m4a|mov|mp3|webm|mp4)$ {
	rewrite ^ https://mydomain.com$request_uri?
	permanent;
	access_log off;
}

Serve 404 assets:

location ~* \.(jpe?g|gif|css|png|js|ico|pdf|m4a|mov|mp3|webm|mp4)$ {
	expires 24h;
	log_not_found off;
	try_files $uri $uri/ @production;
}

location @production {
	resolver 8.8.8.8;
        proxy_pass https://mydomain.com/$uri;
}

APACHE

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*/(uploads/.*)$ http://mydomain.com/wp-content/$1 [L,R=301,NC]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment