Forum Moderators: phranque

Message Too Old, No Replies

more mod_rewrite help?

my attempt to stop image hotlinking is blocking all images on my own site

         

aquila

3:27 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



Hi, I'm using the following code in a .htaccess file to stop people leeching images in my /images directory:


RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://mydomain.(.*)/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://(.*).mydomain.(.*)/.*$ [NC]
RewriteRule (.*\.(gif¦jpg¦avi¦mpg¦GIF¦JPG)$) [mydomain.com...] [R,NC]

Although this works very well, it's also blocking the images on my own webpages too!
Presently I've had to set RewriteEngine to off, coz I don't want my website to look stupid.
Can anyone see why this could be happening? If you need to look at the html of my webpage, I'll be happy to post the url.

jdMorgan

12:53 am on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



aquila,

Welcome to WebmasterWorld [webmasterworld.com]!

It looks to me like a case of over-design. Try simplifying it to:


RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://.*example\. [NC]
RewriteRule \.(gif¦jpg¦avi¦mpg)$ /public/notallowed.gif [NC]

That will do everything your original code does, but allow images to been displayed on your "home page" at whatever.example.whatever/ and example.whatever/ - which your code blocks. The [NC] flag in the RewriteRule makes it unnecessary to include upper- and lower-case variations in the pattern.

Regarding the posting of URLs. please review the WebmasterWorld Terms of Service [webmasterworld.com] and our Apache forum charter [webmasterworld.com]. Thanks!

Jim

aquila

1:38 am on Jan 23, 2004 (gmt 0)

10+ Year Member



Thanks for the tip Jim. I'll give that a go ;)

As for the url posting. I wasn't 100% sure, that's why I offered to post instead :)

dcrombie

11:09 am on Jan 23, 2004 (gmt 0)



Aren't there some problems with redirecting to a different filetype
ie. from JPEG to GIF?

jdMorgan

5:27 pm on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dcrombie,

That's a good point; While substituting .jpeg for .gif or vice-versa usually works, you can't expect a redirect from .jpg to .html to work -- Images are loaded by <SRC> tags and pages are loaded by clicking <A HREF> links, so the two won't mix.

But in this case, the stated problem is that the correct image won't load for accesses which are not blocked, so the image type is not being changed anyway.

Jim