Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess to prevent View Image Full Size"

Can this be done

1:55 pm on Aug 9, 2009 (gmt 0)

5+ Year Member

I'm using an htaccess to prevent hotlinking, which works great. The images still show up as Google thumbnails (which I dont mind) but what I don't like is the "View Image Full Size" - ie the image on its own, rather than on my website.
What I'd like is to have something where, if the address bar is just the image or ends in .jpg - ie www.example.com/images/myimage.jpg - it would redirect to the home page or another page instead.

I tried this but it didn't work. Thanks

RewriteCond %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?\.jpg [NC]
RewriteRule http://www.example.com [L]

3:18 pm on Aug 9, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

You've got a requested URL-path where a referrrer should be, so that won't work. Put your own domain name where I show "example.com" here.

RewriteCond %{HTTP_REFERER} !^(http://(www\.)?example\.com/.*)?$ [NC]
RewriteRule ^images/[^.]+\.jpg$ http://www.example.com/ [R=302,L]

Best practice would really be to return a 403-Forbidden response, and then provide text links to your home page, category pages, site help, site map (HTML table of contents), and site search pages (as applicable) on your custom 403 error page. But the above snippet does what you asked for.


4:01 pm on Aug 9, 2009 (gmt 0)

5+ Year Member

Thanks for the reply.

Only problem is - now I don't get any of my images that I do want to appear within any of my pages.
In other words, www.example.com/images/whatever.jpg does redirect.
But none of the images in www.example.com/page1.htm show up. I tried replacing the redirect with a Forbidden response, but same thing.

This is what I have

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?example\.com/.*$ [NC]
RewriteRule \.(gif¦jpe?g¦png)$ - [F]
RewriteRule ^pictures/[^.]+\.jpg$ - [F]

4:42 pm on Aug 9, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

You have to specify *precisely* what requested image URL-paths should redirect and which should not, and precisely under what conditions.

Be aware that RewriteConds apply only to the single RewriteRule that follows them. So your second rule in your immediately-previous post is applied unconditionally, regardless of referrer; No images below /pictures will be accessible under any conditions whatsoever -- all access to that path is forbidden.

The tough part of this is not writing the code, it is specifying which "groups" of URLs should be redirected and which should not, and under what conditions. The URL-paths must be described in terms that are testable by regular-expressions, and the conditions must be described in terms of request characteristics testable by RewriteConds looking at HTTP request and server-environment variables. There's no "magic" at work here, we have to specify everything correctly and completely.

Also, it does no good to cite "images on my pages" without telling us what the paths to those images are. So please provide multiple and thoroughly-representative example images paths which should and should not be redirected if the referrer is not your own site. We need a list.

Remember, that in the code I posted, mod_rewrite is looking only at the requested image URL and the referrer to make its decisions, and has no other way of determining "what you want." So your example list must illustrate as many characteristics of open-access and restricted-access image paths as possible. If you omit any image-path cases in the code, then those images will either be redirected when they should not be, or they won't be redirected when they should be.


7:04 pm on Aug 9, 2009 (gmt 0)

5+ Year Member

Sorry - I just don't know how to specify that when the call for 'example.com/pictures/pic.jpg' comes from my html code <img src=...) that it's good, but to deny access or do a redirect when the call for 'example.com/pictures/pic.jpg' comes from the browser address bar.
1:10 pm on Aug 10, 2009 (gmt 0)

5+ Year Member

The htaccess seems to be working now - apparently the browser was pulling images from the cache. Once I cleared the cache, all seems fine. Thanks

Featured Threads

Hot Threads This Week

Hot Threads This Month