允许/拒绝使用 .htaccess 进行图像热链接

发布于 2024-07-30 18:03:08 字数 263 浏览 2 评论 0原文

所以我在我的网站 .htaccess 文件中添加了这个,以防止来自所有其他域的图像、JS 和 CSS 的热链接。

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteRule \.(gif|jpe?g|js|css)$ - [F,NC,L]

问题:我如何有选择地允许一两个域进行热链接?

So I've got this in my site .htaccess file to prevent hotlinking of images, JS and CSS from all other domains.

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteRule \.(gif|jpe?g|js|css)$ - [F,NC,L]

Question: How would I selectively allow one or two domains to hotlink?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

累赘 2024-08-06 18:03:09

只需在您要允许的每个域的 RewriteRule 之前添加另一个条件即可。

RewriteCond %{HTTP_REFERER} !friendlysite\.com [NC]  

(大概你不关心请求是通过http还是https还是其他方式,所以你可以忽略它以使其更通用)

Just add another condition before the RewriteRule for each domain you want to allow.

RewriteCond %{HTTP_REFERER} !friendlysite\.com [NC]  

(presumably you don't care if the request is via http or https or whatever, so you can leave that out to make it more generic)

一枫情书 2024-08-06 18:03:08
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?otherdomain\.com [NC]
RewriteRule \.(gif|jpe?g|js|css)$ - [F,NC,L]

正如这句话所说,会起作用的。

“Refererr 并不是什么都不是,Referer 与 mydomain 不匹配,Referer 与 otherdomain 不匹配。

如果您试图执行相反的操作(将一组域列入黑名单,防止热链接),您会执行类似的操作

RewriteCond %{HTTP_REFERER} ^http://(www\.)?baddomain1\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?baddomain2\.com [NC]
RewriteRule \.(gif|jpe?g|js|css)$ - [F,NC,L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?otherdomain\.com [NC]
RewriteRule \.(gif|jpe?g|js|css)$ - [F,NC,L]

Will work, as this says.

"Refererr is not nothing, and referer is not matching mydomain and referer is not matching otherdomain.

If it were the case that you were trying to do the opposite (blacklist a set of domains from hotlinking) you'd do something like

RewriteCond %{HTTP_REFERER} ^http://(www\.)?baddomain1\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?baddomain2\.com [NC]
RewriteRule \.(gif|jpe?g|js|css)$ - [F,NC,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文