Protect Uploads in Multisite

You’ve got some issues in your underlying objective …

For example, I have a filed called 40c.jpg located at localhost/files/2011/07/40c.jpg

OK, this makes sense and is where the image should be in the first place.

I want the file to show up only when called by HTML on the local domain (here: localhost). So not on any other site on the WordPress Network, any other domain or when accessed directly (IE. visiting localhost/files/2011/07/40c.jpg).

This is where you have a problem …

When you reference an image in an HTML block, the HTML isn’t calling/serving the image. Your server gives the HTML to the browser, the browser parses it and sees the image reference, then the browser goes back out and asks your server for the image.

For example, here’s the actual network activity for images on my own site:
Example Screenshot

For that big code image in the header, my server returns this markup:

<img width="600" height="300" src="http://mindsharestrategy.com/files/2010/12/code.png" class="attachment-Slideshow" alt="code" title="code">

My browser sees that, and makes a separate request for the image, downloads the image from the server, and renders it on the screen.

If I wanted to limit who can load this image, I would add on the server some code to intercept the request, check the value of the Referer header, and either return the content or not.

This handling is 100% outside of WordPress. No WP_Rewrite. No plugins. Nothing done with your theme. You might be able to get Apache to do this with a combination of changes to .htaccess and httpd.conf, but that’s a sysadmin task beyond the scope of this site.

Leave a Comment