4

I'm using Apache and lets say there is a file at "images/sample.jpg".

  • With .htaccess, how can i make it appearing on the website and then prevent the direct access by url (for example, direct url like "http://www.abc.com/images/sample.jpg") ?

  • If possible, i also want the solution to affect on all sub-folders below the ".htaccess" file.


Additional Note:
After one day of getting below answers, i found all solutions logically work, but problem with Firefox. I mean, the below answers are giving the solution while testing on every browser but not with Firefox.

夏期劇場
  • 17,821
  • 44
  • 135
  • 217

5 Answers5

3

This is called "hotlinking" - I suggest reading up on it first, as there is more to it than just a line or to of code in your .htaccess. This page is decent: http://altlab.com/htaccess_tutorial.html

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://i.imgur.com/qX4w7.gif [L]

This will show an image from imgur, however I sometimes like to go further and host alternative images from my own host - if it's a competitor, for instance, you can have a lot of fun ;)

LeonardChallis
  • 7,759
  • 6
  • 45
  • 76
  • Please, I'm not so clear about "imgur". What is it about? Do i need to use that line? – 夏期劇場 Apr 19 '12 at 19:49
  • This is why I posted the link to the site (though many others can be found through Google). This is just an example - imgur is just an image hosting web site. You can put any URL there, even your own image - but you'd need to have a different rule to stop "recursion". – LeonardChallis Apr 19 '12 at 19:51
  • I'm testing on my "localhost/example" web folder and it is not working like what i want. I mean, all images on my site are showing 'imgur warning image'. – 夏期劇場 Apr 19 '12 at 19:54
  • As I said, the code was an example... but did you change mysite.com to your own URL? – LeonardChallis Apr 19 '12 at 19:59
  • I'm using `RewriteCond %{HTTP_REFERER} !^http://(.+\.)?localhost\test/ [NC]`. Then on my site, all images are showing just 'imgur image'. Not their proper image. I just want to PREVENT ONLY WHEN the image is called by direct url. But ALLOWED to be appearing on the website. – 夏期劇場 Apr 19 '12 at 20:06
  • Yes, I understand what you want, but there's a mistake in your code. Get rid of `\test` and try that - but please... go read that URL I posted and you will be able to figure this stuff out yourself :) – LeonardChallis Apr 19 '12 at 20:08
  • Same result however i change the server path. And actually, the line `RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]` is not needed as i test. This only line `RewriteCond %{HTTP_REFERER} !^$` is enough. But whatever, the result is same as i mentioned above. – 夏期劇場 Apr 19 '12 at 20:32
  • Please jump to my next thread with more clear explaination http://stackoverflow.com/questions/10236717/htaccess-how-to-prevent-a-file-from-direct-url-access – 夏期劇場 Apr 19 '12 at 21:03
  • The problem is in `RewriteCond %{HTTP_REFERER} !^$`. WITHOUT IT, it works well. – 夏期劇場 Apr 19 '12 at 22:05
  • So, you fix hotlinking by hotlinking ? #nice – Julien Palard Jul 19 '14 at 16:33
1

Use this chunk of code to prevent hotlinking:

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com.*$ [NC] 
RewriteRule \.(gif|jpg)$ - [F]

This could help you out: http://www.selfseo.com/story-18469.php

Ruslan Osipov
  • 5,655
  • 4
  • 29
  • 44
1

Not entirely sure but this should guide you:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteRule (images/sample.jpg)$ - [R=404,L]
PadraigD
  • 641
  • 5
  • 13
  • Please jump to my next thread with more clear explaination http://stackoverflow.com/questions/10236717/htaccess-how-to-prevent-a-file-from-direct-url-access – 夏期劇場 Apr 19 '12 at 21:04
  • All solutions (including this one) normally work, but problem with Firefox. – 夏期劇場 Apr 20 '12 at 10:32
1

There is a LOT more going on to prevent hotlinking of images, than an .htaccess rule. However, the basis of what you'd want in your htaccess to prevent image hotlinking is :

RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$      [NC]
RewriteRule .*\.(jpg|jpeg|png|bmp)$ - [F,NC]

Apache ReWrite guide

PenguinCoder
  • 4,335
  • 1
  • 26
  • 37
  • Please jump to my next thread with more clear explaination http://stackoverflow.com/questions/10236717/htaccess-how-to-prevent-a-file-from-direct-url-access – 夏期劇場 Apr 19 '12 at 21:04
  • All solutions (including this one) normally work, but problem with Firefox. – 夏期劇場 Apr 20 '12 at 10:30
1

Based on your comments looks like this is what you need:

RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost/ [NC] 
RewriteRule \.(jpe?g|gif|bmp|png)$ - [F,NC]

I have tested it on my localhost and it seems to be working fine.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Please jump to my next thread with more clear explaination http://stackoverflow.com/questions/10236717/htaccess-how-to-prevent-a-file-from-direct-url-access – 夏期劇場 Apr 19 '12 at 21:04
  • All solutions (including this one) normally work, but problem with Firefox. – 夏期劇場 Apr 20 '12 at 10:32