How can I prevent other sites from linking to my javascript files?

You can use your .htaccess file to restrict the domain.

  • Selective hotlinking prevention through .htaccess

  • Prevent Hotlinking of Image, Script, CSS etc Using .htaccess


Cretae a .htaccess in the root of you site folder (for apache or IIS with ISAPI_Rewrite)

Replace mysite.com with your domain remebering that all . have to be backslashed in the RewriteCond and replace with a page you want to send them to when there trying to steal your bandwidth

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\. (js|json)$ http://mysite.com/theif.txt

and add theif.txt to your site with the code below (any site trying to steal your code will send all there users to http://www.yourhtmlsource.com/sitemanagement/bandwidththeft.html lol

top.location = "http://www.yourhtmlsource.com/sitemanagement/bandwidththeft.html";

They will soon unlink your script from there page


You may be able to prevent them using your javascript files directly, but there is no guaranteed way to prevent them from copying your files and use them manually, i can already see few answers you may want to try this:

  • use your javascript files by a php file:

    header("content-type: text/javascript");
    if(isset($_GET["name"]) && strpos("yourdomain.com", $_SERVER['HTTP_REFERER']))
         echo(file_get_contents("hidden_path_to_js/".$_GET["js_name"]."js"));
    else
         die("access denied");
    

In above sample you going to check if refer address is your website or not, so for using your js file

<script src="get_js_file.php?js_name=jquery"></script>