Forum Moderators: phranque
At the moment my htaccess file is this:
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif¦jpg¦png)$ - [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¦jpg)$ http://www.mydomain.com/thief.gif [R,L]
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mydomain.com(/)?.*$ [NC]
RewriteRule .*\.(gif¦jpg¦jpeg¦bmp¦png)$ 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¦jpg¦jpeg¦bmp¦png)$ 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]
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¦jpg¦jpeg¦bmp¦png)$ [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¦jpg¦jpeg¦bmp¦png)$/ && ! ~ /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
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mydomain.com(/)?.*$ [NC]
RewriteRule (/(gif¦jpg¦jpeg¦bmp¦png)$/ &&! ~ /thief\.gif/) http://www.mydomain.com/thief.gif [R,NC]
It's causing an internal server error.
Have I missread you instructions?
Cheers,
Matt
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¦jpe?g¦bmp¦png)$ http://www.mydomain.com/thief.gif [NC,R,L]
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com [NC]
RewriteRule \.(gif¦jpe?g¦bmp¦png)$ /thief.gif [NC,L]
HTH,
Jim
[edited by: jdMorgan at 4:35 pm (utc) on June 27, 2003]
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>
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?
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