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?

0


source to share


2 answers


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

+5


source


This is a fantastic tutorial that I keep in my bookmarks all the time. It can describe how to prevent hotlinking on images, but it adapts easily to other file types such as javascript or CSS.



See what you need. :)

+1


source







All Articles