Forum Moderators: phranque
I'm trying to find a way to thwart hotlinkers selectively on a forum site I visit... as it's become an issue where it's done to impersonate and for other malicious reasons.
On this forum, anonymous posts allowed and I have a domain I use to host all of my images I hotlink myself.
Unfortunately, all tutorials and how-tos with examples tend to take the broad stroke approach... ie, it prevents hotlinking from your domain for the entire site, like Myspace. All or nothing.
The below is a completely fabricated URL just used as an example, as the threads on the other forum follow this URL pattern.
[webmasterworld.com...]
Would go to a comments section for one headline, for example. Anything can follow after it, sometimes it's pagination code, sometimes it's a switch to show all comments on one page, but as simple as it can be, that's what the URL would look like.
I hotlink from my own domain frequently on this forum, but again, it can just be one or two threads here and there where others are hotlinking and I'd like to be able to thwart them.
How would I go about writing a ModRewrite rule for this?
This is what I currently have...
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?webmasterworld\.com/apache/12345678
RewriteRule .*\.(jpeg¦jpg¦JPG¦gif¦bmp¦png)$ /replace.jpe [L]
(My little trick to use a different extension not listed in the rewrite rule. This prevents infinite loops.)
In place of HTTP_REFERER, I've had REQUEST_URI instead and that did not work. I've tried wildcards at the end of the URL, omitting the last digit, and that didn't work either.
What happens is that instead of just rewriting for that one thread, it rewrites for the entire site... exactly what I'm trying to avoid.
I'm rather surprised the answer has eluded me so far, I've not seen any sites that show how to do a simple rewrite, but with that twist... Is it even possible?
Thanks for the help...
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?webmasterworld\.com/apache/12345678 [NC]
RewriteRule .*\.(jpe?g¦gif¦bmp¦png)$ /replace.jpe [L]
I take it this may be an unusual request after all.
Similarly, if the alternate image is cached by the visitor's browser, then that alternate image will be served from the browser's cache, no matter what page the visitor is viewing.
So the problem is that selective anti-hotlinking image replacement isn't possible if the image is cacheable. And you certainly don't want your images to be non-cacheable, since that's a major part of your server load.
That's one reason that I recommend the same response to all unwelcome accesses: Return a 403-Forbidden response and be done with it...
As a consolation prize, here is how you can make your code serve any type of 'replacement' image you like, without having to worry about the looping problem:
RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/replace\.jpg$
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?webmasterworld\.com/apache/12345678 [NC]
RewriteRule \.(jpe?g¦gif¦bmp¦png)$ /replace.jpg [NC,L]
RewriteEngine on
#
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?webmasterworld\.com/apache/12345678 [NC]
RewriteRule \.(jpe?g¦gif¦bmp¦png)$ - [NC,F]
Jim