apache开启防盗链方法 不指定

jed , 2012-3-17 13:57 , 服务器技术 , 评论(0) , 阅读(7862) , Via 本站原创 | |
    Ps:现在盗链比较严重,所以很多站长都要学会防盗链,一下是我查看的一些方法,效果不错


8. 防止盗链
8.1 启用 Rewrite 模块
此模块默认没有启用


#a2enmod rewrite    8.2 配置
修改/etc/apache2/sites-available下对应站点的配置文件, 将


AllowOverride None 修改为

AllowOverride All 8.3 控制文件
在站点的根目录下创建 .htaccess 文件 内容如下:


RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydebian.com/.*$ [NC] RewriteRule?.*\.(jpg|jpeg|gif|png|bmp|rar|zip|exe)$ ?http://www.mydebian.com/err.png?[R,NC] 在站点的根目录下,创建err.png图片,当发生盗链时,对应显示将替换为err.png图片。



来源:http://www.mkv8.com/?p=208

现在做网站,都会遇到盗链的问题,包括图片盗链、音乐或视频文件(如mp3、Flash等)盗链.相信很多朋友都曾遇到过网站内文件特别是图片被盗链的情 况烦。所谓盗链,是指对方网站直接链接您网站上的文件,而不是将其置于自己的服务器上,一般而言,盗链的对象大多为较耗带宽的大体积文件,如图片、视频 等,从某种意义上说,这事实上造成了让您为其访问流量买单:不仅您的服务器带宽被无任何回报地占用,而且,往往会在很大程序上影响您网站的访问速度。
下面我们就以图片为例子来说明这个.我们要实现的是不仅屏蔽/禁止非本网站的盗链访问,还将盗链请求转给自身的网站,让读者知道谁是真正的发布者(图片源网站)
设置 .htaccess 禁止图片盗链
RewriteEngine on
打开重写url
RewriteCond %{REQUEST_URI} !^/allow/.*$
排除的url.这个相当的重要,因为后面的referer过滤时会过滤这些.所以打开这个可以"盗链",这样才能让用户正确的显示出错的网站.
RewriteCond %{REQUEST_FILENAME} \.(gif|jpeg|png|jpg)$ [NC]
以上为判断是否为图片文件:您也可以根据自己的需要设置更多的文件类型。也可以是exe之类
RewriteCond %{HTTP_REFERER} !^$
上面这一行意在允许空“HTTP_REFERER”的访问,即允许用户在浏览器地址栏中直接输入图片地址时图片文件的显示。建议,如果强迫必须具有“HTTP_REFERER”才能访问,可能会带来某些问题,比如直接在url地址栏中输入地址
RewriteCond %{HTTP_REFERER} !dzhope\.com [NC]
RewriteCond %{HTTP_REFERER} !google\.com [NC]
RewriteCond %{HTTP_REFERER} !baidu\.com [NC]
设置允许访问的HTTP来源,包括网站自身、Google、Baidu、等。
RewriteRule (.*) /allow/error.gif [R=301,NC,L]
将不满足referer条件的访问重定向至error.gif。有没有发现error.gif位于允许“盗链”的目录allow中,要相当注意,不然,警告信息和图片将无法在对方网站上显示。
如上,就实现了我们要的功能。不仅屏蔽/禁止非本网站的盗链访问,还将盗链请求转给自身的网站,让读者知道谁是真正的发布者(图片源网站)
其他类型文件的防盗链设定
如果您的网站上存在其他类似体积较大较耗费带宽的文件如flash、mp3被其他网站盗链,可以同样采取上述策略,比如说,对Flash文件,可用类似如下的设置:
RewriteCond %{REQUEST_URI} ^/allow
RewriteCond %{REQUEST_FILENAME} \.swf$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !php-oa\.org [NC]
RewriteRule (.*) /allow/error.swf [R=301,NC,L]
当然,需要事先有声明版权信息的flash文件“error.swf”。其他如mp3文件、压缩文件(zip或rar)盗链的设置和这个一样。


来源:http://www.javascriptkit.com/howto/htaccess10.shtml


Preventing hot linking of images and other file types
Note: This portion of tutorial written by JavaScript Kit

In the webmaster community, "hot linking" is a curse phrase. Also known as "bandwidth stealing" by the angry site owner,  it refers to linking directly to non-html objects not on one own's server, such as images, .js files etc. The victim's server in this case is robbed of bandwidth (and in turn money) as the violator enjoys showing content without having to pay for its deliverance. The most common practice of hot linking pertains to another site's images.

Using .htaccess, you can disallow hot linking on your server, so those attempting to link to an image or CSS file on your site, for example, is either blocked (failed request, such as a broken image) or served a different content (ie: an image of an angry man) . Note that mod_rewrite needs to be enabled on your server in order for this aspect of .htaccess to work. Inquire your web host regarding this.

With all the pieces in place, here's how to disable hot linking of certain file types on your site, in the case below, images, JavaScript (js) and CSS (css) files on your site. Simply add the below code to your .htaccess file, and upload the file either to your root directory, or a particular subdirectory to localize the effect to just one section of your site:

RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC] RewriteRule \.(gif|jpg|js|css)$ - [F]Be sure to replace "mydomain.com" with your own. The above code creates a failed request when hot linking of the specified file types occurs. In the case of images, a broken image is shown instead.
Serving alternate content when hot linking is detected
You can set up your .htaccess file to actually serve up different content when hot linking occurs. This is more commonly done with images, such as serving up an Angry Man image in place of the hot linked one. The code for this is:

RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC] RewriteRule \.(gif|jpg)$ http://www.mydomain.com/angryman.gif [R,L]Same deal- replace mydomain.com with your own, plus angryman.gif.

Tags: ,
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]