Preventing hotlinking with a .htaccess file in a cloud


Hotlinking can be referred as the linking directly to a non-html object on other servers, this would include images, movies, files etc. This could impact the bandwidth usage as well and in some cases, GPU as well if you are on the Grid. You will be able to prevent the requests on the cloud server using a .htaccess file.

So what is .htaccess? .htaccess is a configuration file which is used on a web server that runs on Apache Web Server software. So when a .htaccess file is placed in a directory which will load through Apache Web Server, only then the .htaccess file will be detected and then you can execute it by the Apache Web Server software. And you can use the .htaccess files to alter the configuration of its Apache Web Server software where you can enable/disable additional functionality and features that the Apache Web Server software will offer. These facilities include basic redirect functionality, say that if you find an error like 404 files not found or any more advanced functions such as content password protection or image hotlink prevention.



Preventing hotlinking with a .htaccess file in a cloud


Before one needs to start there are some requirements one must have

The FTP login credentials as well as the FTP client.

A plain text editor


If you are already having a .htaccess file you simply need to add one of these two snippets of code to the existing file or you need to create a new plain text document with the code and then you need to upload it to the domain directory or a particular subdirectory for localizing the effect to just one of the websites or a section of one of these websites:


For simply stopping the hotlinking procedure you just need to add the following to the .htaccess file. Now you need to replace on the 3rd line with a domain name of yours.

RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://(www\.)example.com/.*$ [NC]

RewriteRule \.(gif|jpg|jpeg|bmp|zip|rar|mp3|flv|swf|xml|php|png|css|pdf)$ - [F]


For Serving the Alternate Content when the hotlinking is detected. You will be able to set up the .htaccess file for actually displaying the different contents when hotlinking is being attempted. This is being done commonly with the images suggesting the displeasure of the activity. Once again you need to replace the example.com on line 3 as well 4 with the domain name of your own.

 

RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://(www\.)example.com/.*$ [NC]

RewriteRule \.(gif|jpg)$ http://www.example.com/angryman.gif [R,L]