.ht访问文本文件和重定向的黑名单



我有一个站点,只有在传入请求出现在黑名单文件中时才想重定向它们。我想将文件存储在公共 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/hellohttp://example.com/hello应重写为https://example.com/hello

谢谢 豪伊

  1. DocumentRoot以外的任何路径中创建文本文件。每行应包含 2 列,由单个空格分隔。第一列应该有你列入黑名单的路径谷,第二列应该有值1。示例如下:

    cat /tmp/blacklist.txt
    hello 1
    about 1
    contact 1
    portfolio 1
    
  2. 然后在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>
    
  3. 重启阿帕奇服务器

  4. 最后,在您的站点根目录 .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]
    

最新更新