Forum Moderators: phranque

Message Too Old, No Replies

ModRewrite Rule with a Twist

Trying to make hotlinking rewrite less broad

         

inkrat

1:24 am on Jan 12, 2009 (gmt 0)

10+ Year Member



I've been searching for an answer to this one for a while, tried to study the apache documents for a way to make this happen but have not had much luck in doing so.

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

inkrat

1:29 am on Jan 12, 2009 (gmt 0)

10+ Year Member



Apologies, accidntally changed a line in trying to sanitize my rewrite code for the forum.

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?webmasterworld\.com/apache/12345678 [NC]
RewriteRule .*\.(jpeg¦jpg¦JPG¦gif¦bmp¦png)$ /replace.jpe [L]

g1smd

1:32 am on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note that
jpeg¦jpg¦JPG¦JPEG
simplifies to just
jpe?g
when used with the
[NC]
flag. :-)

inkrat

2:12 am on Jan 12, 2009 (gmt 0)

10+ Year Member




Nothing wrong with making it more efficient. ;) Remainders of two separate cut and pastes from examples.

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.

jdMorgan

6:13 am on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem here is that once the image is cached by the viewer's browser, it will be displayed to that viewer no matter what page its URL is included on.

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]

But again, once the replacement image is cached by the client, it will continue to show up until it expires in the client cache. Which is why I prefer:

RewriteEngine on
#
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?webmasterworld\.com/apache/12345678 [NC]
RewriteRule \.(jpe?g¦gif¦bmp¦png)$ - [NC,F]

As usual, replace the broken pipes "¦" with solid pipes; Posting on this forum modifies the pipe characters.

Jim

g1smd

8:24 pm on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Among other things, notice that the
[NC]
is correctly shown on *BOTH* lines.