Prevent other sites to link to my javascript files

In order to prevent other websites to hotlink to your files, you can enter something similar to the below code in your .htaccess file.

# .htaccess file
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
# THESE URLS ARE ALLOWED TO LOAD THE FILES
RewriteCond %{HTTP_REFERER} !^(https|http)://(.+\.)?allowed-domain.com/.*$ [NC]
# BLOCK ACCESS TO THESE FILETYPES
RewriteRule \.(js|css)$ - [F]

When an attempt to load the file is made from any domain other than the ones listed in the .htaccess file, the request will be rejected!



You could even add more fun to it by redirecting them to another file instead. This technique is mostly used to prevent hostlinking images. Instead of the original image, an alternative image will be displayed when hotlinking is detected.

# .htaccess file
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^(https|http)://(.+\.)?allowed-domain.com.*$ [NC]
RewriteRule \.(png|jpg|gif)$ https://redirect-url.com/unhappy.jpg

Created on : 2016/12/17 - Last updated on : 2016/12/17

Back