Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deny referrals from all domains except one

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.

like image 339
user1059511 Avatar asked Sep 13 '25 06:09

user1059511


1 Answers

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.)
like image 140
Ilmari Karonen Avatar answered Sep 16 '25 14:09

Ilmari Karonen