简体   繁体   English

Apache 2.4-如何将图像重定向到php文件?

[英]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. 我需要将所有图像重定向到一个php文件,包括路径和文件名。

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 假设我的域名是example.com,我可能拥有https://example.com/art/logo.png,并希望将其重定向到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. 因此,我将强制将任何请求重定向到将显示图像的php文件。 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' 我还希望html文件使用此重定向并使用php文件显示图像,例如,包含的'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. 我已经使用引用标头方法进行了mod_rewrite操作,但是它不起作用,我不确定为什么。 I am assuming that the https protocol doesn't use a referrer in the header. 我假设https协议在标头中不使用引荐来源网址。

For the php I need to know how I can stop someone from hotlinking my images and from just downloading them as files. 对于php,我需要知道如何才能阻止某人热链接我的图像以及仅将其下载为文件。 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. 我的网站使用HTTPS或在HTTPS上运行。 A lot of solutions for hotlinking online only shows examples using http. 许多在线热链接解决方案仅显示使用http的示例。

1. Can you determine wether the request is from a user or a bot? 1.您能否确定请求是来自用户还是机器人?

NYeah, more or less^^ You can use the UserAgent-Header field to determine if the request was sent via a browser or not. NYeah,或多或少^^您可以使用UserAgent-Header字段确定请求是否通过浏览器发送。 But then the question is do you want to blacklist all site-rippers (like HTTrack) or whitelist all common Browsers? 但是接下来的问题是,您是否要将所有站点窃听者(如HTTrack)列入黑名单或将所有常见的浏览器列入白名单? Both will be an incomplete and pretty long list. 两者都是不完整且很长的清单。

2. How to rewrite all images to your php-file 2.如何将所有图像重写为您的php文件

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 如果被调用的URI模式遵循您指定的'/path/image.png'到/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 . 第一个括号(.+)包括服务器名之后的部分,直到最后一个文件名,因此在示例/path/example/image.png它将包含path/example The second bracket (.+) only includes the file name, so in the example /path/example/image.png it would contain image.png . 第二个括号(.+)仅包含文件名,因此在示例/path/example/image.png它将包含image.png Now both values are assigned to the target-URL --> 现在,两个值都分配给目标URL->

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

Hopefully I got your question right... 希望我能正确回答你的问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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