使用 rewrite 技术实现 Apache 防盗链
2012-06-27 16:07
741 查看
本文参考自以下文章:
/article/3489372.html
本地有两个虚拟目录:localhost/t/和localhost/js/,设置Apache使localhost/js/目录中的文件不能引用localhost/t/目录下的图片文件。
1.使Apache的rewrite module可用。打开httpd.conf文件,将#LoadModule rewrite_module modules/mod_rewrite.so前的#去掉。
允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”)
将
2.将以下配置写入.htaccess文件,放入根目录或者图片所在的目录即可。
重新启动Apache服务器即可。
关于源代码的解释:
1.RewriteCond %{HTTP_REFERER} !^$ [NC]
允许空“HTTP_REFERER”的访问,即允许用户在浏览器地址栏中直接输入图片地址时图片文件的显示。一般而言,这是可选的,不过,建议这么设置,如果强迫必须具有
“HTTP_REFERER”才能访问,可能会带来某些问题,比如说在用户通过代理服务器访问时。
2.RewriteCond %{HTTP_REFERER} !http://localhost/t/ [NC]
设置允许访问的HTTP来源。
3.RewriteRule .*\.(gif|jpg|png)$ http://localhost/t/error.png [R,NC]
定义被盗链时替代的图片,让所有盗链 jpg、gif、png 等文件的网页,显示根目录下的error.png 文件。注意:替换显示的图片不要放在设置防盗链的目录中,并且该图片文件体
积越小越好。另外本人在使用中还发现一个问题,即替换图片为jpg文件时显示出现问题,而使用png文件时则程序正常运行。
4.说明一下其中的R、NC 和 L
R 就是转向的意思
NC 指的是不区分大小写
L 的作用是指明本次转向到此结束,后续的转向不受先前判断语句的影响
/article/3489372.html
本地有两个虚拟目录:localhost/t/和localhost/js/,设置Apache使localhost/js/目录中的文件不能引用localhost/t/目录下的图片文件。
1.使Apache的rewrite module可用。打开httpd.conf文件,将#LoadModule rewrite_module modules/mod_rewrite.so前的#去掉。
允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”)
将
# AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride None修改为:
# AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride All
2.将以下配置写入.htaccess文件,放入根目录或者图片所在的目录即可。
# 防盗链配置 RewriteEngine On RewriteCond %{HTTP_REFERER} !^$ [NC] RewriteCond %{HTTP_REFERER} !http://localhost/t/ [NC] RewriteRule .*\.(gif|jpg|png)$ http://localhost/t/error.png [R,NC]
重新启动Apache服务器即可。
关于源代码的解释:
1.RewriteCond %{HTTP_REFERER} !^$ [NC]
允许空“HTTP_REFERER”的访问,即允许用户在浏览器地址栏中直接输入图片地址时图片文件的显示。一般而言,这是可选的,不过,建议这么设置,如果强迫必须具有
“HTTP_REFERER”才能访问,可能会带来某些问题,比如说在用户通过代理服务器访问时。
2.RewriteCond %{HTTP_REFERER} !http://localhost/t/ [NC]
设置允许访问的HTTP来源。
3.RewriteRule .*\.(gif|jpg|png)$ http://localhost/t/error.png [R,NC]
定义被盗链时替代的图片,让所有盗链 jpg、gif、png 等文件的网页,显示根目录下的error.png 文件。注意:替换显示的图片不要放在设置防盗链的目录中,并且该图片文件体
积越小越好。另外本人在使用中还发现一个问题,即替换图片为jpg文件时显示出现问题,而使用png文件时则程序正常运行。
4.说明一下其中的R、NC 和 L
R 就是转向的意思
NC 指的是不区分大小写
L 的作用是指明本次转向到此结束,后续的转向不受先前判断语句的影响
相关文章推荐
- 关于Apache mod_rewrite的中文配置、使用和语法介绍(实现URL重写和防盗链功能)
- 关于Apache mod_rewrite的中文配置、使用和语法介绍(实现URL重写和防盗链功能)
- 关于Apache mod_rewrite的中文配置、使用和语法介绍(实现URL重写和防盗链功能)
- 关于Apache mod_rewrite的中文配置、使用和语法介绍(实现URL重写和防盗链功能)
- 关于Apache mod_rewrite的中文配置、使用和语法介绍(实现URL重写和防盗链功能
- rewrite 技术实现 Apache 防盗链
- Apache服务器使用.htaccess实现图片防盗链
- Apache中利用mod_rewrite实现防盗链
- Apache rewrite技术实现Apache到lighttpd迁移(转)
- 使用Apache的rewrite技术
- 使用Apache的rewrite技术
- Apache服务器使用.htaccess实现图片防盗链方法教程
- Apache 防盗链(Apache Anti-Leech)技术的简单实现
- 使用Apache的rewrite技术
- (转) Apache rewrite技术实现Apache到lighttpd迁移
- Apache服务器使用.htaccess实现图片防盗链
- 使用Apache的rewrite技术
- Apache rewrite技术实现Apache到lighttpd迁移
- 使用Apache的rewrite技术
- 使用Apache的rewrite技术