News:

The "Support Requests" forum is now viewable by guests.

Main Menu

htaccess

Started by infinity, November 28, 2005, 01:07:40 AM

Previous topic - Next topic

webzone (archived)

adding extensions to the .htaccess protection won't cause a problem. this time, i tested it.

but i still can't seem to get the code allowing multiple referer domains to work as intended.

admin

This is typically because you do not have the correct flags at the end of the rules or conditions (in []) to specify if this is the last rule, etc.

Please see http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html for more information.  You may need to use [OR] on the RewriteCond lines for example.

Thank you,
FreePgs.com Admin

markjay

Example: Your site url is www.example.com. To stop hotlinking images from an outside domain and display an image called nohotlink.jpg instead, use this code in your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/ [NC
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpe?g|gif|bmp|png)$ images/nohotlink.jpg [L]

To allow hotlinking images only if the REFERER (sic) is from a specific directory from your domain:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/dir/ [NC
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpe?g|gif|bmp|png)$ images/nohotlink.jpg [L]

To stop hotlinking images from specific outside domains only, named badsite.net and badsite.com:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(www\.)?badsite\.net/ [NC,OR
RewriteCond %{HTTP_REFERER} ^http://(www\.)?badsite\.com/ [NC
RewriteRule \.(jpe?g|gif|bmp|png)$ images/nohotlink.jpg [L]

Limit bandwidth usage by returning a 403 forbidden code. Replace the last line of the previous examples with this line:
RewriteRule \.(jpe?g|gif|bmp|png)$ - [F]

Warning: Do not use .htaccess to redirect image hotlinks to another HTML page or server that isn't your own (such as this web page). Hotlinked images can only be redirected to other images, not to HTML pages.

As with any .htaccess rewrites, you may block some legitimate traffic (such as users behind proxies or privacy firewalls) using these techniques.

source: http://altlab.com/htaccess_tutorial.html

webzone (archived)

The two first examples mostly come to the exact same thing that the code I provided does. In fact, my code is mostly a compressed version of this.

By the way, there are many missing ] in the samples. It should be [NC], for instance.

markjay

i had tried your sample but your first sample blocks all images and the second can be still hotlinked...
you should test your sample thoroughly

webzone (archived)

#35
i did all the necessary tests already. i admitted soon after i added my post that the first example might not work. however, the second one works well and really disables hotlinking.

Quoteyou should test your sample thoroughly
or maybe should you use my samples better?
i mean : it works. i made all necessary tests and even more. i'm using it (temporarily) right now.

---
correction (5:55) : I also tested the code with more than one RewriteCond line and it works. My code can really restrict to directories and can allow multiple hosts (by using more than one RewriteCond in a row). Note however, that there is a little difference between the tested code and the published one (this error has been fixed since) where the dash (-) in RewriteRule was changed to a backslash.