首页 文章

使用.htaccess限制从单个引荐来源网域访问网页

提问于
浏览
0

我想限制对网站的访问,只允许来自单个域的引荐来源 . 我无法让.htaccess文件正常工作 . 假设我是从http://domainname.com引用 - 将允许访问 . 或http://subdomain.domainname.com - 允许访问 .

但任何其他引用者(或键入URL)将阻止,并指向“拒绝访问”页面 .

代码如下(注意我需要允许从domainname.com上的任何引用页面访问

RewriteEngine On
RewriteBase /

# allow these referers to passthrough
RewriteCond %{HTTP_REFERER} ^http://(protect|unprotected)\.domainname\.com
RewriteRule ^ - [L]

# everybody else receives a forbidden
RewriteRule ^ - [F]

ErrorDocument 403 /forbidden.html

1 回答

  • 1

    HTTP referer标头仅表示请求来自 . 例如 . www.example.net 某些网页中有链接时

    <a href="http://www.example.com/some/path>Click here</a>
    

    那么请求将是 http://www.example.com/some/path ,referer头将包含来自 www.example.net 的URI .

    如果您在没有特定引用者的情况下阻止任何请求,那么任何直接请求也将被阻止 . 另请注意,referer标头由客户端发送,因此,它不是可靠的指示器 .

    另一个警告是,根据Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content- 5.5.2 Referer,客户端可以发送 partial-URI ,它根本不包含域名 .


    要回答您的问题,如果您想允许来自 domainname.com 或其任何子域的请求,您可以检查

    RewriteCond %{HTTP_REFERER} ^http://(?:.*\.)?domainname\.com
    RewriteRule ^ - [L]
    
    RewriteRule ^ - [F]
    

    或者相反,当你否定它时,禁止

    RewriteCond %{HTTP_REFERER} !^http://(?:.*\.)?domainname\.com
    RewriteRule ^ - [F]
    

    要检查cond1或cond2或cond3中的多个条件之一,必须将RewriteCondornext|OR 标志一起使用,例如:

    RewriteCond %{HTTP_REFERER} ^http://(?:.*\.)?nature\.com [OR]
    RewriteCond %{HTTP_REFERER} ^http://(?:.*\.)?adclick\.g\.doubleclick\.net [OR]
    RewriteCond %{HTTP_REFERER} ^http://(?:.*\.)?onepointedpixel\.com
    RewriteRule ^ - [L]
    

相关问题