While Dreamhost offers it’s customers an easy way to prevent hotlinking via the web panel, that method is only suitable for basic websites and beginner webmasters that don’t already rely on .htaccess for URL rewriting and other applications often used in CMS / blogging software.

Also the web panel method to prevent hotlinking doesn’t provide users with very many options for controlling hotlinking.

The following examples are methods of preventing hotlinking using .htaccess files.

Blocking all domains from hotlinking
The following code will return a 403 Forbidden error instead of the requested file, unless the referrer is yourdomainatDH.com, which should be changed to your domain:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomainatDH\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpeg|jpg|gif|png)$ - [F]

Blocking Specific Domains from Hotlinking:

The following code will return a 403 Forbidden error instead of the requested image, but only when the image is requested by hotlinkingsite.net or hotlinkingsite.com:

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(www\.)?hotlinkingsite\.net/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?hotlinkingsite\.com/ [NC]
RewriteRule \.(jpeg|jpg|gif|png)$ - [F]

Blocking All Domains with Exceptions
The following code will produce a 403 Forbidden error instead of the requested image, unless requested from yoursiteatDH.com or myspace.com Note: one of the allowed sites should be your domain.

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomainatDH\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?myspace\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpeg|gif|png)$ - [F]

Note: In the above examples only images with lower-case file extensions are being protected. To protect other resources, such as videos, add additional extensions to the Rewrite Rule parentheses block.

For more info and other options check out Preventing hotlinking on the Dreamhost wiki. If you aren’t familiar with editing .htaccess files you can read more about them here.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • Ma.gnolia
  • StumbleUpon

No Responses to “Stop Hotlinking via .htaccess”  

  1. No Comments

Leave a Reply



About DreamNut

DreamNut is an unofficial DreamHost blog with hints, tips and video tutorials.

Use DreamHost promo code MAXSAVINGS at Dreamhost to get up to $50 off DreamHost web hosting.


Archives

Social Bookmarking