How to block website visitors from accessing image files?

1

1

SCENARIO

I am hosting a website on an Apache server, where the images are saved on an img folder.

I have already solved the issue of users accessing folders they are not intended to, placing some index.html files on such folders.

So, for instance, if a user accesses the folder website.com/img/, he will be redirected to website.com.

PROBLEM

I would like to also redirect the user when he access an image, and so, avoid making it possible to see the image and to download it.

Thus, I would like to redirect the user to website.com when he or she accesses website.com/img/image1.jpg.

Is there a way to achieve this via .htaccess? Or is there any other possibility available?

kouk

Posted 2015-09-18T10:04:45.043

Reputation: 13

This question is on-topic on [webmasters.se] site. You might find many similar ideas there yet before asking. For example, this: http://webmasters.stackexchange.com/q/10838 (although it is not exactly the same)

– miroxlav – 2015-09-18T11:01:39.367

What exactly is the point of an image if a visitor cannot see the image. If you display the image to me in my browser, I will be able to download it, there isn't a way to stop that. – Ramhound – 2015-09-18T11:22:31.100

@miroxlav while this might also be on topic on webmasters it does not appear to be off-topic here, which would be the criteria for us migrating it without OPs input. If kouk wants it migrated then fine but currently it is good here. See this question for our stance on migrations when questions are on topic in multiple sites: http://meta.superuser.com/questions/2644

– Mokubai – 2015-09-18T11:35:54.493

Thanks for your help, I didn't know about the existence of the Webmasters site. Given that this question has already been answered, I will post future questions there – kouk – 2015-09-19T11:26:29.950

Answers

0

I guess you have the apache already configured so that the images cannot be accessed?

However, the most easy way to accomplish this is to create a .htaccess in your image folder with:

deny from all

When a user is blocked like this, he receives the HTTP-Error 403, you can add a script to this by adding to .htaccess following:

ErrorDocument 403 /restricted.php

In this script you are able to handle the redirect:

<?php    header('Location: website.com');    ?> 

h0ch5tr4355

Posted 2015-09-18T10:04:45.043

Reputation: 883

0

Use an index.php file with a redirect header :

$ cat img/index.php
<?php
  header('Location: /');
?>

Or use http_redirect() function.

levif

Posted 2015-09-18T10:04:45.043

Reputation: 141