基于apache伪静态重定向简单实例

1.图片防盗

  r.com/img.php:

<img src="http://r.com/img/logo.jpg"/>

.htaccess:
当来源不为空且不是以localhost开始,则判定为非法
RewriteCond %{HTTP_REFERER} !^$ #
RewriteCond %{HTTP_REFERER} !^localhost
RewriteRule ^(.*).(jpg|jpeg|png|gif)$ - [F,NC]

访问如下url能够显示图片
r.com/img/logo.jpg 不是以localhost开始
localhost/rewrite/img/logo.jpg 来源为空
localhost/rewrite/img.php 来源是 Referer: http://localhost/,不符合第二条件
无法显示的
r.com/img.php

2.限制访问者的IP列表

 添加扩展RewriteMap,txt内容如下

127.0.0.1 true
r.com true
http://r.com true
localhost true

 RewriteMap ipwhitelist txt:D:phpstudy_proWWW ewriteipwhitelist.txt #这一配置放在apache配置文件中,然后重启

RewriteBase /
RewriteCond ${ipwhitelist:%{REMOTE_ADDR}} !^true$
RewriteCond ${ipwhitelist:%{REMOTE_HOST}} !^true$
RewriteRule ^ - [R=400]

测试

r.com/img.php 访问正常

localhost/rewrite/img.php 提示400错误,用localhost地址访问时系统记录的ip是 ::1,映射文件里面没有这个值

原文地址:https://www.cnblogs.com/xiangdongsheng/p/14054138.html