我有一个站点,只有在传入请求未出现在黑名单文件中时才想重定向它们。我想将文件存储在公共 html 目录之外,这样它就会像var/www/blacklists/example.com.blacklist
一样,内容看起来像
hello
about
contact
portfolio
如果我的网站example.com
并且用户访问https://example.com/goodbye
,只要"再见">不在黑名单上,它就会重定向到https://somethingelse.com/goodbye
。
如果用户访问https://example.com/hello
并且"hello">在黑名单上,它将正常解决。
此外,所有解析的请求通常需要强制使用 SSL。example.com/hello
或http://example.com/hello
应重写为https://example.com/hello
谢谢 豪伊
-
在
DocumentRoot
以外的任何路径中创建文本文件。每行应包含 2 列,由单个空格分隔。第一列应该有你列入黑名单的路径谷,第二列应该有值1
。示例如下:cat /tmp/blacklist.txt hello 1 about 1 contact 1 portfolio 1
-
然后在
httpd.conf
内插入一个新的RewriteMap
行,如下所示:# First, we configure the "default" to be a very restrictive set of # features. <Directory /> # Existing directives here # followed by this RewriteMap RewriteMap blacklist "txt:/tmp/blacklist.txt" </Directory>
-
重启阿帕奇服务器
-
最后,在您的站点根目录 .htaccess 中具有以下规则:
RewriteEngine On RewriteCond ${blacklist:$0} !=1 RewriteRule .+ https://somethingelse.com%{REQUEST_URI} [L,NE,R=302] RewriteCond %{HTTPS} off RewriteCond ${blacklist:$0} =1 RewriteRule .+ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]