简体   繁体   中英

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

I have some jquery plugins hosted on my server, but I'd like it to be available only to my visitors.. I'm paranoid other websites might just link to my js files and steal my bandwidth. How would you solve this problem?

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>

you could always run the javascript through a PHP file using .htaccess to route all requests to it. If the user-agent is different from your website, then it can send a 403, otherwise it could use a file_get_contents to return the file.

hope that helps

useful htaccess template: CheckSpelling on Options -Indexes Options +FollowSymlinks

DirectoryIndex index.php

RewriteEngine on
RewriteBase /js/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /js/index.php?p=$1 [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM