Learn how to prevent people from hotlinking your images with htaccess.
For those of you who don't know what hotlinking is, it's when somebody takes an image uploaded on your server and displays it on their site. This will use up your bandwidth. For example, if somebody finds an image from your site on Google Images, the user may display the image on their site. This will use up the resources on your server. I will show you how to prevent people from hotlinking your images with htaccess. To start, open your .htaccess file. If you don't have one create it and upload it to the root directory of your server. If your computer won't allow you to save the file as .htaccess, save it as htaccess.txt and rename it once it's been uploaded to your server. Put the following code in the .htaccess file you just opened/created. Make the changes described below before saving the file.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mysite\.com [NC]
RewriteRule .*\.(jpe?g|gif|png)$ - [F]Put the URL of your site where it says mysite.com. Make sure you put a backslash (\) before a period (.). On the last line you can set which image extensions you wish to block from hotlinking. By default .jpg, .gif, and .png are blocked. You can add another extension if you wish. Just make sure you separate extensions with a |. You can allow other sites to hotlink your images as well by adding another line and entering the URL
If you do not want to block all sites on the internet from hotlinking your images and only block one or more site from hotlinking your images, use the following code:
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http(s)?://(www\.)?mysite\.com [NC]
RewriteRule .*\.(jpe?g|gif|png)$ - [F]Change mysite.com to the site you wish to block from hotlinking your images. Remember, as with the code above you can change the image extensions if you wish to block other extensions. If you wish to prevent multiple sites from hotlinking your images, change [NC] to [NC,OR] on every additional line but the last one. For example if I wanted to stop gmail.com and google.com from hotlinking my images I would use the following code:
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http(s)?://(www\.)?google\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http(s)?://(www\.)?gmail\.com [NC]
RewriteRule .*\.(jpe?g|gif|png)$ - [F]Any questions or comments about preventing image hotlinking? Comment on this article.
Note: Some web hosts may not allow you to use .htaccess.
