Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite - hot link blocking of images with redirect in .htaccess

block works, image redirect fails



5:05 pm on Aug 20, 2006 (gmt 0)

5+ Year Member

Hi - I've read the source Apache docs, the tutorials here and old threads and I guess I'm just too dumb to get it.

I block hot linked images but so many people are too stupid to understand the hotlinked image is not displaying my server kicks 403 after 403 day after day. I want to try the strategy of sending the embedded image request to a new image with notice of the hotlink and our domain. And I can not for the life of me get it to work.

Here is what I've had and tried:

Original (worked - kicked 403s)

RewriteRule .*\.(gif夸pg夸peg在mp如ng)$ - [F,NC]

New - not working - kicking 400s (w/o broken pipes of course)

RewriteRule .*\.(gif夸pg夸peg在mp如ng)$ /images/stolenpicture.jpg [NC,L]

In one thread here I saw a note about blocking your redirect with your own rule so I tried to be sure I was not blocking myself with above rule but I still kicked 400 errors (just blocked jpgs and sent to gif):

RewriteRule \.(jpg)$ /images/stolenpicture.gif [NC]

Short version of full original code in .htaccess

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mydomain.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://(www\.)?google.com(/)?.*$ [NC]
RewriteRule .*\.(gif夸pg夸peg在mp如ng)$ - [F,NC]

Again, this is the original and it worked. I replaced last line with redirect to stolen pic and I get 400 errors.

This is on a Mambo site and after these rules run another set continue for SEF URLS:

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*) index.php

Are these breaking the image redirect?



6:49 pm on Aug 20, 2006 (gmt 0)

5+ Year Member

(Tried to edit my first post but time allowed expired.)

Well, not sure I know why, but I've got this working and will share my solution. I'm now using:

RewriteRule .*\.(gif夸pg夸peg在mp如ng)$ stolenpicture.$1 [NC]

as the last line and this rule seems to work just fine.

If you are going to lift this for your own use make sure you replace the broken pipes with solid pipes (above the "\" key on your keyboard)!

stolenpicture = file name of sub'd image.

It is now in my root (was it a path problem all along?)

Nobody steals my non-gif or jpg files, so I've got a stolenpicture.gif and stolenpicture.jpg sitting in the root. The stolenpicture.$1 looks to source type and replaces it with the same type. So amazingpicture.jpg gets redirected to stolenpicture.jpg

Am I understanding this much correctly?

It does appear to work at least - even if my brain does not.


7:33 pm on Aug 20, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

The most likely cause of your problem is that the pattern in your rule would also match the replacement image URL, thus causing an 'infinite' loop. To avoid that, and to allow the replacement image to be placed and the mod_rewrie code to work in any directory, simply exclude the replacement image from being rewritten (or redirected, as the case may be):

RewriteCond %{REQUEST_URI} !^/psth_to_replacement_image
RewriteRule \.(gif夸pe?g在mp如ng)$ /path_to_replacement_image.$1 [NC]

Note also the tweaks to the rule to eliminate unneeded stuff.



8:54 pm on Aug 20, 2006 (gmt 0)

5+ Year Member

Many thanks Jim!

Featured Threads

Hot Threads This Week

Hot Threads This Month