Protecting your images, CSS and Javascript files from external sites with Apache?
I came across a site that demonstrated a Javascript library and asked you not to link to a Javascript file directly from your site. This is a reasonable request. It didn't actually occur to me to do this instead of hosting it myself, but I think it will try to save bandwidth in any way.
This got me thinking: does Apache (in a shared hosting environment) have any simple means to either prevent this, or at least make it a little harder by looking at HTTP_REFERRER or your favorites? Or maybe even just make sure you have a PHP session?
source to share
Using htaccess file you can do this.
Just create a .htaccess file in the directory of the files you want to protect, in it:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com(/)?.*$ [NC]
For more information and some other things you can do to prevent hotlinking at the webserver level, see the Dev Papers article on Preventing Hotlinking
source to share