简体   繁体   中英

Apache 2.4 - How to redirect images to a php file?

I need to redirect all images to a php file, including the path and file name.

Imagine my domain is example.com I might have https://example.com/art/logo.png and want to do redirect this to https://example.com/scripts/image_loader.php?a:art&b=logo.png

So, I would force any request to be redirected to a php-file which will display the image. I am doing this to control who can access the images and also to prevent hotlinking. I also want html-files to use this redirect and use the php file to display the image, eg for included 'img src=/art/logo.png'

I already did the mod_rewrite using the referral header method but it's not working and I am not sure why. I am assuming that the https protocol doesn't use a referrer in the header.

For the php I need to know how I can stop someone from hotlinking my images and from just downloading them as files. Can I determine if the user is a person or a bot or some kind of site ripper?

I know I can't stop a person from downloading images. I just want to make it harder for them. So, they cannot just easily download them or use a site ripper software.

Any suggestions would be great. My website uses or runs on HTTPS. A lot of solutions for hotlinking online only shows examples using http.

1. Can you determine wether the request is from a user or a bot?

NYeah, more or less^^ You can use the UserAgent-Header field to determine if the request was sent via a browser or not. But then the question is do you want to blacklist all site-rippers (like HTTrack) or whitelist all common Browsers? Both will be an incomplete and pretty long list.

2. How to rewrite all images to your php-file

RewriteEngine On
RedirectRule ^/(.+/(.+\.(jpe?g|gif|png|tif|bmp))$ /scripts/image_loader.php?a:$1&b=$2

This rewrites all requests for standard-images, if the called URI-pattern follows your specified '/path/image.png' to /scripts/image_loader.php?a:path&b=image.png

update

RedirectRule ^^/(.+/)+(.+\.(jpe?g|gif|png|tif|bmp))$ /scripts/image_loader.php?a:$1&b=$2

The first bracket (.+) includes the part after the servername up to the last file name, so in the example /path/example/image.png it would contain path/example . The second bracket (.+) only includes the file name, so in the example /path/example/image.png it would contain image.png . Now both values are assigned to the target-URL -->

/scripts/image_loader.php?a:path/example&b=image.png

Hopefully I got your question right...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM