Forum Moderators: phranque

Message Too Old, No Replies

.htaccess

Stopping bandwidth stealing!

         

shady

12:59 am on Mar 21, 2003 (gmt 0)

10+ Year Member



A site is displaying my images using a direct link

<img src=www.mysite.co.uk/images/xxx.jpg>

I wish to display a specific image instead of the image they are attempting to access.

I've tried the following in .htaccess, to check for their domain as referer and act upon it, but it just displays the famous X (image missing):

RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER} ^http://(www\.)?theirsite.com/.*$ [NC]
RewriteRule \.(gif¦jpg)$ [mydomain.co.uk...] [R,L]

I don't really know what I am doing with .htaccess, so thanks in advance for your help!

Birdman

1:20 am on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here ya go.

Stop direct linking to images [webmasterworld.com]

shady

2:23 pm on Mar 29, 2003 (gmt 0)

10+ Year Member



Thanks Birdman that works fine.
I wish to ensure that I am not affecting people with referals switched off. How do I turn referals off so I can test this?

jdMorgan

3:38 pm on Mar 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



shady,
I'd suggest:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.co.uk [NC]
RewriteRule \.(gif¦jpg)$ /images/7638.gif [NC,L]

This is a more general solution, internally redirecting all non-blank referrers that are not from your own domain to an alternate image.

I also recommend having an alternate image for each image type, gif, jpg, png, etc. and using this rewrite rule instead, along with the RewriteConds above:


RewriteRule \.(gif¦jpg)$ /images/7638.$1 [NC,L]

This will redirect each image request to an alternate image of the correct (same) type.

You can use wannabrowser (do a search) to test with any referrer you like.

Change the broken vertical pipe "¦" characters above to solid vertical pipe characters, and make sure these is a space preceding the "!" characters, as usual.

Jim

shady

3:56 pm on Mar 29, 2003 (gmt 0)

10+ Year Member



I have, I think, implemented a solution that is working. Please advise me if you think there are any problems with it.

The reason for this is to redirect to a specific image, only if the referer is a specific site (for which I'll use www.specific-site.com in the example):

RewriteEngine On
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER} ^http://www\.specific-site\.com/* [OR]
RewriteCond %{HTTP_REFERER} ^http://specific-site\.com/*
RewriteRule ^.*$ [redirect-image-url.co.uk...]

This I believe will only have effect if referers is enabled in browser/firewall AND the referer begins with [specific-site.com...] or [specific-site.com...]

Look ok?

jdMorgan

4:27 pm on Mar 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



shady,

Your code will redirect all file types, including .html, to a .gif file, which some browsers may not be able to handle. Is that the desired behaviour?

Jim

shady

4:54 pm on Mar 29, 2003 (gmt 0)

10+ Year Member



Yes thats fine, so long as it only affects the one site which is using my images on their site.
What about redirecting a .jpg to a .gif - will browsers handle that or could I setup a different redirect for *.jpg and *.gif.

jdMorgan

10:37 pm on Mar 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



shady,

I recommend having an alternate image of each type, so the browsers don't get confused when they ask for a jpg and you send them a gif. The RewriteRule I posted above does that - the $1 on the right side of the rule takes the value of whatever matched the parenthesized group on the left; If a gif is requested, a gif is returned, except that it is your special replacement gif for the hotlinkers. Same thing for a jpg request, your alternate jpg will be returned to hotlinkers.

I presume from what you are saying that you want other sites to be able to hot-link your images, just not that particular one. Frankly, I don't allow any hotlinking, so the code I posted says "If the referrer is not blank and that request was not referred from my site, then redirect it." By saying "not referred from my site" instead of naming a specific site, I avoid having to keep adding site after site as I discover other hotlinkers - It is a maintenance issue.

Note that if you are serving alternate images, you need to keep the file size small, otherwise you won't save anything on bandwidth. You can use the alternate image to say "this image stolen from www.mysite.com" or to play a trick on the thief "Special 2-for-1 offer - And FREE shipping on all merchandise!", etc. Or you can just block the request using the

RewriteRule \.(gif¦jpg)$ - [F]

construct in the thread referenced by Birdman in post #2 above, thus returning a 403-Forbidden response code, and optionally a custom-403 page.

With mod_rewrite, it all depends on what you want to do - It's a very handy power-tool.

Jim