【问题标题】:Block everyone, except visitors from specific referrer [closed]阻止所有人,除了来自特定推荐人的访问者[关闭]
【发布时间】:2013-03-27 23:31:19
【问题描述】:

在 htaccess 中,我如何阻止所有访问者,包括机器人,除了来自特定域或使用特定用户代理的访问者?

我必须保护每个页面不被访问,除了几个允许的页面。

每个被阻止的人都应该收到自定义通知。

谢谢

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    首先允许包含禁止消息的公共页面。然后使用适当的RewriteConds 允许具有特定引荐来源和用户代理的请求,并将状态403 发送给其他所有内容

    RewriteEngine on
    
    # allow public pages
    RewriteRule ^forbidden.html$ - [L]
    RewriteRule ^public1.html$ - [L]
    RewriteRule ^public2.html$ - [L]
    
    # serve everyone from specific-domain or specific-user-agent
    RewriteCond %{HTTP_REFERER} ^https?://www.specific-domain.com [OR]
    RewriteCond %{HTTP_USER_AGENT} ^specific-user-agent$
    RewriteRule ^ - [L]
    
    # everybody else receives a forbidden
    RewriteRule ^ - [F]
    
    ErrorDocument 403 /forbidden.html
    

    【讨论】:

    • 对于#allow public pages 部分,您将如何允许子目录或子目录中的文件,即RewriteRule ^/lib/css/$ - [L]RewriteRule ^/lib/css/styles.css$ - [L]
    • @Keefer 规则/正则表达式很好,除了前导斜杠。另请参阅此答案stackoverflow.com/a/42969607/1741542
    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多