Deny referrals from all domains except one

走远了吗. 提交于 2019-12-01 07:34:58

问题


Is it possible to accept traffic from only one domain, ideally using a .htaccess file? I want my site to only be accessible via a link on another site I have.

I know how to block one referring domain, but not all domains

RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} otherdomain\.com [NC]
RewriteRule .* - [F]

this is my full rewrite code:

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !domain\.co.uk [NC]
RewriteRule .? - [F]

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I think it is working, but none of the assets are getting loaded and I get a 500 error when I click on another link.


回答1:


Make that something like:

RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !yourdomain\.com [NC]
RewriteCond %{HTTP_REFERER} !alloweddomain\.com [NC]
RewriteRule .? - [F]

The first RewriteCond checks that the referrer is not empty. The second checks that it doesn't contain the string yourdomain.com, and the third that it doesn't contain the string alloweddomain.com. If all of these checks pass, the RewriteRule triggers and denies the request.

(Allowing empty referrers is generally a good idea, since browsers can generate them for various reasons, such as when:

  • the user has bookmarked the link,
  • the user entered the link manually into the address bar,
  • the user reloaded the page,
  • the browser is configured not to send cross-site referrer infromation, or
  • a proxy between your site and the browser strips away the referrer information.)


来源:https://stackoverflow.com/questions/9572604/deny-referrals-from-all-domains-except-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!