Description of the problem
You have a site xyz.mysite.com on Cloudflare (ip 1.1.1.2) which sits in front of your real server (ip 9.9.9.9). A different page, www.othersite.com is showing the contents of your site and you want to block it.
Possibilities
I see a few options on what could be happening:
- www.othersite.com is resolving directly to your real ip 9.9.9.9 You get the connections directly from the clients
- www.othersite.com is proxying your content by connecting directly to 9.9.9.9
- www.othersite.com is proxying your content by connecting through Coudflare (1.1.1.2)
If the issue is the first one, you could simply filter based on the host header used, blocking them:
RewriteEngine on
RewriteCond expr "%{HTTP_HOST} != %{SERVER_NAME}"
RewriteRule .* - [F,L]
Or just redirect them to your real site:
RewriteEngine on
RewriteCond expr "%{HTTP_HOST} != %{SERVER_NAME}"
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [R=permanent,L]
If the issue is the second one, you could block the IP addresses they are using, althouth they might then change how they connect to bypass it. So instead of blocking the addresses you don't want to connect, you can simply allow just those that are allowed. Since you only want those accesses from Cloudflare, you can filter that. Cloudflare publishes their IP ranges at https://www.cloudflare.com/ips/ and your problem the one stated on Only allow certain IP addresses to access site with mod_rewrite? or Redirect a range of IPs using RewriteCond.
Thus your .htaccess may end up as
RewriteEngine on
RewriteCond expr "! -R '173.245.48.0/20'"
RewriteCond expr "! -R '103.21.244.0/22'"
RewriteCond expr "! -R '103.22.200.0/22'"
RewriteCond expr "! -R '103.31.4.0/22'"
RewriteCond expr "! -R '141.101.64.0/18'"
RewriteCond expr "! -R '108.162.192.0/18'"
RewriteCond expr "! -R '190.93.240.0/20'"
RewriteCond expr "! -R '188.114.96.0/20'"
RewriteCond expr "! -R '197.234.240.0/22'"
RewriteCond expr "! -R '198.41.128.0/17'"
RewriteCond expr "! -R '162.158.0.0/15'"
RewriteCond expr "! -R '104.16.0.0/12'"
RewriteCond expr "! -R '172.64.0.0/13'"
RewriteCond expr "! -R '131.0.72.0/22'"
RewriteCond expr "! -R '2400:cb00::/32'"
RewriteCond expr "! -R '2606:4700::/32'"
RewriteCond expr "! -R '2803:f800::/32'"
RewriteCond expr "! -R '2405:b500::/32'"
RewriteCond expr "! -R '2405:8100::/32'"
RewriteCond expr "! -R '2a06:98c0::/29'"
RewriteCond expr "! -R '2c0f:f248::/32'"
RewriteRule .* - [F,L]
(ranges of Cloudflare as of November 2020)
This would work for case #1 as well.
Finally, in the third case, they could be hitting through Cloudflare. You could block them based on the CF-Connecting-IP
header provided by Cloudflare, but they should really get blocked at Cloudflare level rather than at your server. I suspect you won't be on this third case.