【转】使用Apache防盗链设置和自定义错误页面

使用Apache防盗链设置和自定义错误页面

本文转载于http://www.discuz.net/thread-885395-1-1.html

先来看看Apache的防止特定文件禁止下载的方法:
在httpd.conf文件最下面添加

  1. #Apache禁止类型下载
  2. <directory X:/X/X>
  3. <FilesMatch "\.(html|htm|jpg|php|css|gif)">
  4. Order Deny,Allow
  5. Deny from all
  6. </FilesMatch>
  7. </directory>
复制代码

说明:

  1. <directory X:/X/X>
复制代码

这里写你要保护的目录的绝对路径,比如C:/web/templates
这样C:/web/templates下的制定类型的文件将无法下载。可以来保护你的模板文件。

指定文件类型

  1. <FilesMatch "\.(html|htm|jpg|php|css|gif)">
复制代码

里面的类型可以自己添加。

下面是防盗链的简单设置:
在你需要保护的网站域名定义字段内添加

  1. NameVirtualHost *:80
  2. <VirtualHost *:80>
  3. 在此添加
  4. DocumentRoot X:/你网站所在目录/X
  5. ServerName www.你的网址.com
  6. </VirtualHost>
复制代码

  1. SetEnvIf Request_URI "/logo(.)+" local_ref=0
  2. SetEnvIfNoCase Referer "^http://bbs.wanshy.com" local_ref=1
  3. SetEnvIfNoCase Referer "^http://boke.wanshy.com" local_ref=1
  4. SetEnvIfNoCase Referer "^http://blog.wanshy.com" local_ref=1
  5. SetEnvIfNoCase Referer "^http://buy.wanshy.com" local_ref=1
  6. SetEnvIfNoCase Referer "^http://www.wanshy.com" local_ref=1
  7. <FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif|ma|png|wav|midi|wma|mb|swf|fla)">
  8. Order Allow,Deny
  9. Allow from env=local_ref
  10. </FilesMatch>
复制代码

第一行是定义文件名为logo.*的文件可以排除在防盗链之外,这样可以和联盟网站传输logo图片。
第2-6行是定义排除的网站,在这里排除的网站不受防盗链影响。
第7行是定义文件类型,参照上面的防止特定文件禁止下载

然后就是自定义错误页面:

  1. ErrorDocument 404 http://bbs.wanshy.com/index.php
  2. ErrorDocument 403 http://bbs.wanshy.com/index.php
复制代码

格式为

  1. ErrorDocument   错误代码 绝对地址
复制代码

这样就能自定义你自己的错误页面了。

最后附上Apache下绑定多域名的方法:
在的最后加上

  1. NameVirtualHost *:80
  2. <VirtualHost *:80>
  3. DocumentRoot X:/你网站所在目录/X
  4. ServerName www.你的网址.com
  5. </VirtualHost>
复制代码

注意本文所介绍的方法适用于windows主机,其他主机为测试!
所修改添加字段均在Apache的httpd.conf文件里面,.htaccess文件未做测试。

=====================================================

一、自定义错误信息页面,并根据所请求的URL所在的域,跳转到所在域名。
二、自定义APACHE错误信息,指定错误信息页面。配置文件里面有例子信息,去掉下面的注释符号#,激活。

Alias /error/ "/usr/local/apache2/error/"
 
    <Directory "/usr/local/apache2/error">
        AllowOverride None
        Options IncludesNoExec
        AddOutputFilter Includes html
        AddHandler type-map var
        Order allow,deny
        Allow from all
        LanguagePriority en cs de es fr it ja ko nl pl pt-br ro sv tr
        ForceLanguagePriority Prefer Fallback
    </Directory>
 
    ErrorDocument 400 /error/HTTP_BAD_REQUEST.html.var
    ErrorDocument 401 /error/HTTP_UNAUTHORIZED.html.var
    ErrorDocument 403 /error/HTTP_FORBIDDEN.html.var
    ErrorDocument 404 /error/404/index.html
    ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.var
    ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
    ErrorDocument 410 /error/HTTP_GONE.html.var
    ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
    ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
    ErrorDocument 413 /error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
    ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var
    ErrorDocument 415 /error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
    ErrorDocument 500 /error/HTTP_INTERNAL_SERVER_ERROR.html.var
    ErrorDocument 501 /error/HTTP_NOT_IMPLEMENTED.html.var
    ErrorDocument 502 /error/HTTP_BAD_GATEWAY.html.var
    ErrorDocument 503 /error/HTTP_SERVICE_UNAVAILABLE.html.var
    ErrorDocument 506 /error/HTTP_VARIANT_ALSO_VARIES.html.var

三、实现跳转JS。
新建错误信息html页面,页面中关键代码如下:

var ym=document.domain;//获取请求的URL域名+++ 关键 ++++
  var returnid=5;
   function return_prepage(){
      document.getElementById("return_page").innerHTML=returnid+"";
   if(returnid==0){
 
 window.location.href="http://"+ym;  //重定向到请求的域名网站
   }
   returnid=returnid-1;
  if(returnid>-1){
   window.setTimeout("return_prepage()",1000);}
   }
 
  return_prepage()

猜你喜欢

转载自kinglcc.iteye.com/blog/937474
今日推荐