Forum Moderators: phranque

Message Too Old, No Replies

Hotlink protection driving me crazy

         

Echil0n

6:21 pm on Sep 28, 2007 (gmt 0)

10+ Year Member



There's a site ( http://profiles.example.com/5420716 ) which is linking the image http://quux-foo.nu/laptop.jpg from my site. I've been fiddling with .htaccess for hours trying to make apache block hotlinking from example.com and my-space. My config is below, please post any suggestions as o why this won't work.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^stuff.apple.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.quux-foo.nu$
RewriteRule ^(.*)$ http://quux-foo.nu/$1 [NC,R,L]

RewriteCond %{HTTP_REFERER} ^http://(www\.)?my-space\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?example\.com [NC]
RewriteCond %{REQUEST_URI}!^.nolink\.jpg$ [NC]
# RewriteRule ^ContemplatingExistence_1900\.jpg$ nolink.jpg [L]
# RewriteRule ^laptop\.jpg$ nolink.jpg
RewriteRule ^.*\.(swf¦bmp¦gif¦jpg¦jpe¦jepg¦png¦jpeg¦avi¦wmv¦mpg¦mpeg¦wav¦mp3)$ http://quux-foo.nu/nolink.jpg [NC,R,L]

[edited by: jdMorgan at 10:22 pm (utc) on Sep. 28, 2007]
[edit reason] examplified, de-linked. [/edit]

jdMorgan

10:44 pm on Sep 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteEngine on
#
# Externally redirect to canonicalize domain name
RewriteCond %{HTTP_HOST} ^stuff\.apple\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.quux-foo\.nu
RewriteRule (.*) http://quux-foo.nu/$1 [R=301,L]
#
# Stop hotlinking from my-space and example.com
RewriteCond %{HTTP_REFERER} ^http://(www\.)?my-space\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !^/nolink\.jpg$ [NC]
RewriteRule \.(swf¦bmp¦gif¦jp[eg]¦jepg¦jpeg¦png¦avi¦wmv¦mpe?g¦wav¦mp3)$ http://quux-foo.nu/nolink.jpg [NC,R=302,L]

1) Escape literal periods in patterns
2) Do not end-anchor hostnames, or if you feel you must, then append "(:[0-9]+)?$" to accommodate valid port numbers.
3) Use regex to avoid redundant patterns, e.g. "jp[eg]" replaces "jpe¦jpg"
4) Use 301 to canonicalize domain, 302 for replacement image.
5) Replace all broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.
6)Completely flush your browser cache before testing new code, or after successfully loading images using any "allowed" referrers.

Jim

[edited by: jdMorgan at 10:45 pm (utc) on Sep. 28, 2007]

Echil0n

8:07 am on Sep 29, 2007 (gmt 0)

10+ Year Member



Sorry for not obfuscating the domains, and thanks a lot, it's working now =)