如果用户代理是特定类型,则使用 htaccess 仅允许下载文件
我有一个文件夹,里面有一些 zip 文件。我想拒绝所有直接下载它们的请求,除非用户代理是我指定的类型(例如 user-agent :myuseragent
),
这对于 .htaccess 文件怎么可能?
拒绝的请求将重定向到索引文件,而接受的请求将下载该文件。
例如。如果用户尝试 http://download.com/files/myfile.zip 他们会被拒绝但如果用户代理为“myuseragent”的脚本访问相同的 URL,它将下载该文件,
我知道用户代理可能会被欺骗。这只是对文件免费提供的轻微保护
I have a folder with some zip files in it. I want to reject all requests to download them directly unless the user agent is of the type I specify (eg. user-agent : myuseragent
)
How is this possible with .htaccess file?
Rejected requests would redirect to the index file while accepted requests will download the file.
eg. if a user tried http://download.com/files/myfile.zip they would get rejected but if a script with a useragent of 'myuseragent' access the same url, it would download the file
I understand that useragents can be spoofed. It is only mild protection from having the files freely available
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是如何使用 .htaccess 和 mod_rewrite 来做到这一点:
如果用户代理是,上述规则将指示 Apache 对
/files/
文件夹中的任何文件发出 403 Access Denied 响应不等于myuseragent
。如果您想发出 404 File Not Found 响应,请将
[F,L]
替换为[R=404,L]
。如果您想将此类失败的请求重定向到主页,请使用此规则(您可以将重定向从 302 更改为您喜欢的任何其他重定向代码):
Here how you can do it using .htaccess and mod_rewrite:
The above rule will instruct Apache to issue 403 Access Denied response for any file in
/files/
folder if user agent is not equal tomyuseragent
.If you want to issue 404 File Not Found response, replace
[F,L]
by[R=404,L]
.If you want to redirect such failed requests to home page then use this rule (you can change redirect from 302 to whatever other redirect code you like):