Prevent hotlinking with mod_rewrite

PDF
To prevent that someone is hotlinking to your content (images, video ect) you can use mod_rewrite.Create a .htaccess file in the root directory of your webserver (or edit your if you allready have one) and add these lines:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mydomain\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://www.mydomain.com/nohotlink.gif [L]

The example will redirect requests ending with jpeg, jpg, gif, bmp or png and not having mydomain.com, regardless if written in lower or upper case ([NC]), in the referal and not having an empty referal to http://www.mydomain.com/noimage.gif If you rather serve a 403 Forbidden error code you should change the line:

RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://www.mydomain.com/nohotlink.gif [L]
into:
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]
To prevent hotlinking from specific domains, ie facebook and myspace.com, only and allow all other websites to hotlink to your images you need these lines:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?facebook\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?myspace\.com/ [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://www.mydomain.com/nohotlink.gif [L]
 

Please login first before adding a comment.

Search






You are here: Home Howtos and FAQs Apache Prevent hotlinking with mod_rewrite