homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

mod rewrite - hot link blocking of images with redirect in .htaccess
block works, image redirect fails

5+ Year Member

Msg#: 3053604 posted 5:05 pm on Aug 20, 2006 (gmt 0)

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?




5+ Year Member

Msg#: 3053604 posted 6:49 pm on Aug 20, 2006 (gmt 0)

(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.


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

Msg#: 3053604 posted 7:33 pm on Aug 20, 2006 (gmt 0)

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.



5+ Year Member

Msg#: 3053604 posted 8:54 pm on Aug 20, 2006 (gmt 0)

Many thanks Jim!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved