Forum Moderators: phranque
Each line tests for the domain at both www and non-www and on both .com and .co.uk, but which code is optimum, faster, preferred, less side effects, etc? Is it domain1, domain2, or domain3?
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain1\.(com¦co\.uk)/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain2\.com?(\.uk)?/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain3\.co(m¦\.uk)/.*$ [NC]
RewriteRule \.(gif¦jpg¦jpeg¦png)$ - [F,NC]
Fixed typo.
[edited by: g1smd at 10:02 pm (utc) on Oct. 16, 2006]
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} [i][/i]!^http://(www\.)?domain2\.com?(\.uk)? [NC]
RewriteRule \.(gif¦jpe?g¦png)$ - [NC,F]
Escape the literal periods.
"jpg¦jpeg" replaced by "jpe?g"
Whenever you see "^.*" or ".*$", it's a good bet that you can simply delete all three characters, since an un-anchored pattern is equivalent. The only exception is where you wish to make use of the fact that ".*" is the 'greediest' pattern, and you are trying to keep the adjacent match as short as possible.
Change all broken pipe characters "¦" above to solid pipes before use; Posting on this forum modifies the pipe characters.
Jim
[edited by: jdMorgan at 9:53 pm (utc) on Oct. 16, 2006]
If the domain test is unanchored, does that also mean that your code also allows domain2.co.jp and domain2.com.au and so on to match too?
.
Is it possible to over-ride the block to hotlinking for just ONE image on a site?
It is a logo that other sites sometimes display as a clickable link.
I assume put it in a separate folder and have a different ruleset for just that folder.
How do you over-ride a hotlink ban?
Jim
RewriteCond %{REQUEST_URI} !^/images/logo\.gif$
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain2\.(com¦co\.uk) [NC]
RewriteRule \.(gif¦jpe?g¦png)$ - [NC,F]
RewriteCond $1 !^images/logo\.gif$
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain2\.(com¦co\.uk) [NC]
RewriteRule ^([^.]+)\.(gif¦jpe?g¦png)$ - [NC,F]
Change all broken pipe characters "¦" above to solid pipes before use; Posting on this forum modifies the pipe characters.
Jim
^([^.]+\.(gif¦jpe?g¦png))$
That's part of the beauty of the way rules are processed: RewriteRules can back-reference RewriteCond variables, and RewriteConds can back-reference RewriteRule variables. Now if only mod_rewrite would add a "compare-two-variables" function, I'd be perfectly content. But alas, it's not directly supported in mod-rewrite, and can be done only on some servers whose regex libraries support 'atomic' back-references -- FreeBSD with POSIX 1003.2 support, for example, by using the fact that if A+A=A+B, then A+B.
Jim
RewriteCond %{HTTP_REFERER} ^.*other\.site.*$ [NC]
RewriteRule \.(gif¦jpg¦jpeg)$ http://www\.this\.site\.com/replacement\.$1 [R]
I cannot get it to work. I have simplified it. I even made the test for just a particular full URL referer. I made it JPG only. Whatever I did, it never worked.
RewriteCond %{REQUEST_URI} !^/replacement\.
Jim
[edited by: jdMorgan at 4:58 pm (utc) on Oct. 22, 2006]
RewriteCond %{REQUEST_URI} !^/images/hotlink [NC]
RewriteCond %{HTTP_REFERER} ^http://.*that-?site [NC]
RewriteRule \.(gif¦jpe?g)$ http://www.domain.com/images/hotlink/replacement.$1 [NC,L]
If the URL has to be a literal string, does that mean I cannot use the $1 in it too?
Yes, and substitution is a defined term in the Apache mod_rewrite docs.
> If the URL has to be a literal string, does that mean I cannot use the $1 in it too?
No, it doesn't, because $1 through $9, %1 through %9, and any valid %{SERVER_VARIABLE} or ${REWRITE_MAPS} will be substituted into that substitution string (whether that's what you intended or not) before it is used.
Note that the argument on the right side of a RewriteCond is (usually) a regex pattern, while the argument on the left side of a RewriteRule is always a regex pattern.
Jim
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /gadgets/widgets/(abc¦fgh¦rst¦xyz).*\ HTTP/
RewriteRule ^(([^/]*/)*)(abc¦fgh¦rst¦xyz)\.widgets(.*)$ http://www\.domain\.com/$1$3$4 [R=301,L]
Isn't that a literal string too? I'm confused, 'cus that one works.
For efficiency, you want your images marked as cacheable with long expiry times.
But if it is at all possible that a visitor might fetch your image from a hotlink, and then subsequently visit your site to see the real image, then he'd still have the cached alternative image in his browser cache. So, he'd see the alternative image on your site as well, because an internal rewrite won't change the URL, and therefore his browser will use its cached copy.
So this is a relatively rare case where a 302 is often the best response. Set the hotlink alternate image with a short expiry time and/or mark it uncacheable, while leaving your real image cacheable with a long expiry time. That way, if the vistor does come to your site, the alternate image will be replaced by the real image.
Frankly, I usually just serve a 403 response and don't otherwise bother...
If you do want an internal rewrite, then don't include http://www.example.com in the substitution URl, and don't use the [R] flags.
Jim
They don't mind if people host the images elsewhere, they aren't going to follow through on copyright violations as the images belong to the manufacturer, but the distributor is seeing 50% of their bandwidth being sucked by e-bay hotlinking.
If that doesn't help, we'll need more details on precisely *how* it doesn't work:
Jim