允许在 <video> 中播放视频来自我网站的元素,但不允许通过直接链接 [英] Allowing video to be played in <video> element from my website, but not allowing it through direct link
问题描述
我使用了以下技巧:
首先,我禁用了右键单击以防止用户使用 save as
或 get link
使用此 HTML5:
First, I disabled the right click in order to prevent the user from using save as
or get link
using this HTML5:
<body oncontextmenu="return false;">
</body>
第二:我使用了 controlsList="nodownload"
但它的问题仅在 Chrome 58+ 中正常工作,按照 这个,稍后我可能会考虑海关控制,如图所示这里
Second: I used the controlsList="nodownload"
but the problem it works fine ONLY in Chrome 58+ as per this, later on I may consider customs control as shown here
<video width="512" height="380" controls controlsList="nodownload"
poster="https://archive.org/download/WebmVp8Vorbis/webmvp8.gif" >
<source src="videos/289522.mp4" type="video/mp4">
<source src="videos/289522.ogv" type="video/ogg">
<source src="videos/289522.webm" type="video/webm">
Your browser doesn't support HTML5 video tag.
</video>
我仍然需要防止用户下载它以防他通过其他方式获得链接,我发现了一些关于使用.htaccess
的讨论,所以我在videos
文件夹,并尝试了这个:
I still need to prevent the user from downloading it in case he got the link by other means, I found some talks about using .htaccess
, so I created one inside the videos
folder, and tried this:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://my.domain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.my.domain.com/.*$ [NC]
RewriteRule .(mp4|mp3|avi)$ - [F]`
并用作替代方式,此:
<Files "reminder.php">
Order Deny,Allow
Deny from all
Allow from http://my.domain.com/
Allow from http://my.domain.com/
</Files>
但是发生的事情是视频被完全屏蔽了,甚至从我的网站本身也被屏蔽了,我开始收到这个错误:
But what happened is video had been blocked completely, even from my website itself, and I started getting this error:
GET http://my.domain.com/videos/289522.mp4500(内部服务器错误)
GET http://my.domain.com/videos/289522.mp4 500 (Internal Server Error)
推荐答案
这是可能的.
您的 .htaccess 解决方案是个好主意,要使其发挥作用,您的 .htaccess
需要位于包含源视频的子目录中,仅此而已.更新它,使其看起来像这样:
This is possible.
Your .htaccess solution is a good idea, to get it to work, your .htaccess
needs to be in a sub-directory with the source videos, and nothing else. Update it so that it looks like this:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$ [NC]
RewriteRule .(mp4|mp3|avi)$ - [F]`
或者,这也应该有效(完全阻止访问您的视频内容)
Alternatively this should work too (to completley block access to your video conent)
RewriteEngine On
RewriteCond %{REQUEST_URI} .(mp4|mp3|avi)$ [NC]
RewriteRule ^.* - [F,L]
需要同时包含两个代码:
It is required to include both codes together:
RewriteEngine On
RewriteCond %{REQUEST_URI} .(mp4|mp3|avi)$ [NC]
RewriteCond %{HTTP_REFERER} !^http://sample.com/.*$ [NC]
RewriteRule ^.* - [F,L]
<小时>
顺便提一下,当我们谈论 .htaccess
时,请查看此工具以编写和测试您的文件:http://htaccess.mwl.be/.它让您可以查看哪些条件将满足哪些 URL 模式以及结果如何,这比每次进行更改时都部署到服务器要快得多;)
On a side-note, while we're talking about .htaccess
, check out this tool for writing and testing your file: http://htaccess.mwl.be/ . It lets you see which conditions will meet which URL patterns and what the outcome will be, much faster than deploying to your server each time you make a change ;)
这篇关于允许在 <video> 中播放视频来自我网站的元素,但不允许通过直接链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!