2

I want to, using htaccess, allow hotlinking BUT if someone views an image on my site I want to redirect them to a "hosted" version of the image where I display my navigation etc.

So essentially anyone hotlinking from an external site will not be affected but if someone views the image on my server directly i.e. www.domain.com/image.jpg itll redirect. I also want to make sure I can display images correctly from within my site too.

Can anyone help?

EDIT: Here is the code I currently have

RewriteCond %{REQUEST_FILENAME} \.jpg$
RewriteCond %{HTTP_REFERER} =""
RewriteRule ^userpics/covers/(.*).jpg$ /view/$1.html [R=301]

RewriteRule ^view/(.*).html$ /view.html?img=$1 [L]

Thanks

Chris
  • 26,744
  • 48
  • 193
  • 345

3 Answers3

2

Use .htaccess

RewriteEngine on
RewriteCond %{REQUEST_URI} \.(jpg|png|gif)$
RewriteCond %{HTTP_REFERER} ^https?://yoursite\.com/.*$ [NC]
RewriteRule ([^/]+)$ url_to_script?img=$1

You could simplify

RewriteCond %{HTTP_REFERER} ^https?://yoursite\.com/.*$ [NC]

to just

RewriteCond %{HTTP_REFERER} yoursite\.com [NC]

And the rule can be written as

 RewriteRule (.*) url_to_script?img=$1

if you need to include the path to the image in your parameter.

jasonbar
  • 13,333
  • 4
  • 38
  • 46
  • I have tried this and it works if you open the image directly, but if you link arrive at the image via a website link the HTTP_REFERRER is not null so you see the image properly - what can be done about this? – Chris Feb 28 '10 at 20:40
  • @Chris: This will only rewrite if the referrer is your website. If you access the image directly, or if you click a link from another site, it won't do anything except give you the image. I just tried this on my server and it works as expected. Only if I click a link from my own domain am I redirected. – jasonbar Feb 28 '10 at 20:43
  • I have updated my question with what I currently have based on a combination of all answers. It works perfectly except for when a referrer is set, is there another way to do this? Thanks! – Chris Feb 28 '10 at 20:48
  • @Chris: You want to check that the referrer ${HTTP_REFERER} is set to your domain. The condition in my answer does that. – jasonbar Feb 28 '10 at 20:50
  • Jason - if the image was linked to from another site then, say google images, then it wouldnt redirect would it? – Chris Feb 28 '10 at 20:52
  • @Chris: No, if there is no referrer, or the referrer is not your domain, it will not redirect. – jasonbar Feb 28 '10 at 20:54
0

You should use mod_rewrite, if the HTTP_REFERRER is empty then redirect to /image-with-nav.php?image=$1
Try something like this
RewriteEngine on

RewriteCond %{HTTP_REFERER} ^$

.* image-with-nav.php?image=$1 [L]

DCC
  • 249
  • 1
  • 5
  • This is pretty much what I want but if I click a link on a site that takes me directly to the image wouldnt the referrer be set then? – Chris Feb 28 '10 at 20:18
  • Well yes, but that's the trade off, there's no 100% to achieve this. – DCC Feb 28 '10 at 21:27
0

Now, what you need to check is the referrer because the URL is always the URL of your image, since it is a http request. You could go with something like this:

RewriteCond %{REQUEST_FILENAME} \.(jpg|png|gif)$
RewriteCond %{HTTP_REFERER} =""
RewriteRule (.*) http://someurl.com [R=301]
voodoo555
  • 320
  • 1
  • 6