homepage Welcome to WebmasterWorld Guest from 54.237.54.83
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

    
htaccess and hotlinking
strange problem
crowfax




msg:1497758
 1:29 pm on Jun 27, 2003 (gmt 0)

I've just created an htaccess file to block hotlinking of my images. At the moment it just blocks the image, however I would like it to replace the image with my own.

At the moment my htaccess file is this:

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

Which works fine and dandy but doesn't replace the image. So I had a go at the code for that. It didn't work. IE just sat there spinning trying to load the image. My modem lights were going mad downloading but nothing came up on the screen. I've tried 3 different sets of code, all of which have the same results. I'll list them below so you can see what I've tried. I'm out of ideas, can anyone help?


RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif夸pg)$ http://www.mydomain.com/thief.gif [R,L]


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


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

Cheers,
Matt

[edited by: DaveAtIFG at 1:58 pm (utc) on June 27, 2003]
[edit reason] Removed specific domain name [/edit]

 

crowfax




msg:1497759
 1:35 pm on Jun 27, 2003 (gmt 0)

I've just noticed that if you click the link for my thief image it comes up with a Forbidden error. This is not an intended effect of my htaccess file. Would this error still occur with any of the three sets of code I tried? Maybe this is why the thief.gif image doesn't load?

Help!

claus




msg:1497760
 2:44 pm on Jun 27, 2003 (gmt 0)

Hi crowfax welcome to WebmasterWorld :)

First, there's a rule in here that says that Your'e not supposed to publish Your own URLs. Always keemp them like, say, www.thisdomain.com, www.thatdomain.com

Second, there has been a lot of posts on this topic, try the Site Search [webmasterworld.com]

Now for Your question (I did not mean being rude, it was just some info to give You a better experience in here).

if you click the link for my thief image it comes up with a Forbidden error

That's exactly what You have asked the server to do.

RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?thedomain.com(/)?.*$ [NC]
RewriteRule .*\.(gif夸pg夸peg在mp如ng)$ [thedomain.com...] [R,NC]

Line 1 says: if there is a referrer AND**

Line 2 says: if the referrer is not from "thedomain.com"

Line 3 says: THEN all requests for filenames that end in gif, jpg...png should get the file "thedomain.com/thief.gif"

**Note: put an [OR] after the line if You want an "or", like this:

RewriteCond %{HTTP_REFERER}!^$ [OR]

Effect:

a) When clicking the link from WebmasterWorld, the referrer is webmasterworld (#1 is OK, and #2 is OK) - for this reason the file "thief.gif" should be shown

b) However, it does not. That's because the filename ends with "gif" - and the third line tells the server not to deliver such files.

I'd try this - even though it seems a bit more complicated:

RewriteRule (/(gif夸pg夸peg在mp如ng)$/ && ! ~ /thief\.gif/) [thedomain.com...] [R,NC]

Please remember to replace the pipe character (the ) used in this forum with one typed from your keyboard (it should be one solid line, not divided into two parts)

/claus

crowfax




msg:1497761
 3:55 pm on Jun 27, 2003 (gmt 0)

ok so now my htaccess file looks like this:


RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mydomain.com(/)?.*$ [NC]
RewriteRule (/(gif夸pg夸peg在mp如ng)$/ &&! ~ /thief\.gif/) http://www.mydomain.com/thief.gif [R,NC]

It's causing an internal server error.

Have I missread you instructions?

Cheers,
Matt

jdMorgan




msg:1497762
 4:06 pm on Jun 27, 2003 (gmt 0)

crowfax,

The first one looped because you did not exclude the thief image from the rewrite. The second one didn't work because the rules for regex parsing in mod_rewrite are more limited than those in scripting languages.

You could fix it with:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com [NC]
RewriteCond %{REQUEST_URI} !^/thief\.gif$
RewriteRule \.(gif夸pe?g在mp如ng)$ http://www.mydomain.com/thief.gif [NC,R,L]

Alternately, an internal rewrite (which is better IMHO, since it saves an external redirect and the resultant processing of another HTTP request) would be simpler:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com [NC]
RewriteRule \.(gif夸pe?g在mp如ng)$ /thief.gif [NC,L]

Note that I stripped out some redundant ".*" and anchor stuff, and added an [L] flag to speed things up.

HTH,
Jim

[edited by: jdMorgan at 4:35 pm (utc) on June 27, 2003]

crowfax




msg:1497763
 4:20 pm on Jun 27, 2003 (gmt 0)

jdMorgan - your first example causes an internal server error. And you second example does the same as the first 3 that I tried where IE just sits and tries to load them. :(

Anymore ideas?

jdMorgan




msg:1497764
 4:35 pm on Jun 27, 2003 (gmt 0)

Ideas:
  • Check that there is a space before any "!" character (as corrected above - the forum "trims" spaces)
  • Examine your server error log and find out what caused the error.
  • Replace all "" characters with solid vertical pipes from your keyboard (forum modifies these, too)
  • All warranties are null and void if the code is otherwise modified - specifically, the [L] flags.

    Jim
    <added>One more: Use the RewriteCond %{REQUEST_URI} line in both rulesets, in case you've got other code running that does subsequent internal requests.</added>

  • claus




    msg:1497765
     4:47 pm on Jun 27, 2003 (gmt 0)

    Thanks for helping me out Jim :-)

    the rules for regex parsing in mod_rewrite are more limited

    ...i was sceptical about the searchstring myself, but Your solution #1 is of course the way it should have done.

    However, i dont't get it...Am i wrong, or have you been a bit quick in #2, focusing on slimming the code?

    It seems to me, that this example will still loop, as (no matter what) "thief.gif" is still a gif - have you omitted the point by accident or is it me that's missing something?

    Shouldn't there be a line similar to this one in example 2 as well?

    RewriteCond %{REQUEST_URI}!^/thief\.gif$

    -what is the point that i am missing here?

    crowfax




    msg:1497766
     5:18 pm on Jun 27, 2003 (gmt 0)

    tada! It works. jd's first example is fixed and now works a charm.

    jdMorgan




    msg:1497767
     6:43 am on Jun 28, 2003 (gmt 0)

    claus,

    Number two is an internal rewrite - a filename substitution only. As a result, there is no second HTTP request, as there will be with an external 301 redirect - the file substitution takes place entirely within the context of the current HTTP request. Thus, the Conditions and Rule are only processed once, and there is no opportunity for a loop... unless a script or some other "active agent" causes the .htaccess file to be processed again in the context of the same request, or causes an external redirect after the filename substitution takes place; If this happens, the new image is still a .gif, so it gets rewritten again, and again, and you have a loop.

    However, I suggest an internal rewrite in most hotlinking cases, simply because it does not require the cooperation of the user-agent - the brower or 'bot - and it does not "give away" the fact that a rewrite or redirection is taking place; it simply substitutes one image for another.

    If this kind of internal rewrite does not work, it is because some other factor outside the scope of the rewrite ruleset itself is interfering. This can be caused by omitting the [L] flag and allowing the modified URI/filename to be rewritten again by a following rule - this can happen if the rules are in the wrong order and the [L] flag is missing. One way or the other, something has to happen that causes the rules to be processed again in order for a loop to occur; otherwise, the Conds are tested, the Rule is invoked, the filename is changed, the [L] flag stops rewrite processing, and the alternate image file is served.

    HTH,
    Jim

    claus




    msg:1497768
     10:24 am on Jun 28, 2003 (gmt 0)

    Great, i got it now, thanks a lot :)

    Number two is an internal rewrite - a filename substitution only

    - you omitted the [R] flag of course, and i did not realise the effect of this. Don't know why i didn't think along those lines, but now i've actually learned something, that's great.

    /claus

    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