Firewall rules - Allow certain IPs, block all others

Hello, Cloudflare Community,

I’m reaching out to you as I’m unable to find a solution on the web for my issue. We run an API and have recently decided to give Cloudflare CDN a try so that we can lower latencies for users around the globe.

The API is being operated via RapidAPI (an API marketplace), so there are specific IPs that must be allowed on our primary server in order for routing to work. Before utilizing Cloudflare, I simply added these IPs to an Apache htaccess file and that worked perfectly.

However, after switching to Cloudflare, of course, we needed to discontinue that solution and set up a firewall rule to allow certain IPs through the Cloudflare WAF. So, we set up the firewall rule via Security/WAF/Firewall rules for these IPs, writing an expression that says to allow these IPS, or else, block access.

Here is the expression:

(not ip.src in {}) or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne or (ip.src ne

But when this is deployed, the endpoint requests via RapidAPI are blocked (error 1020).

Is there anyone that can help with this?

Thank you.

Additional details:

  • LAMP stack
  • Dedicated Apache server located in Atlanta, GA
  • Ubuntu 20.04

Replace your ors with ands.

Thank you for your help, Sandro, I will try that solution right away.

No worries, you can also simply add all these addresses to your not in.

Thanks so much, I’ve just deployed the rule and am testing it as we speak.

Unfortunately, now we’re getting a 403 error.

Here’s the expression modified after your suggestion of replacing the ‘ors’ with ‘ands’, is this correct?

(not ip.src in {}) and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne and (ip.src ne


I would add these addresses all to the not in. But that 403 will be probably from your server. Can you post a screenshot?

Hi Sandro,

Yes, that is a server error, so I’m thinking that perhaps I should discontinue the entire htaccess code blocks wherein it is stated:

order deny,allow
deny from all

allow from


I can still post a screenshot if you’d like, but I can confirm that is indeed a server error, so, do you think that the redundancy of having an htaccess file coupled with the Cloudlfare WAF rule is causing the issue? If so, we use CI/CD, so I can remedy that fairly quickly.

If your server is only accessible via the Cloudflare proxies, then you do no need the Apache rules.

Also, make sure you are rewriting IP addresses

1 Like

That’s what I was thinking, but I figured it would not hurt - OK, let me try that and see. I appreciate you, Sandro.

Also in regard to including the IPs in the ‘not in’, do you mean with ranges, as only the first batch appears to be eligible for CIDR, as the other IPs are not ranges?


That worked beautifully after removing the unnecessary htaccess blocks.

Since we lost our DevOps guy a few months ago, I’ve had to take over that role - in addition to being the lead developer, so it’s been quite a ride. There’s just as much - if not more - to DevOps as there is to software programming, so your help saved me a great deal of time.

I’m also looking into your additional suggestion regarding rewriting IP addresses.

Thanks so much for your time, I really do appreciate it, Sandro.

I’ll mark this as solved.

1 Like

Pleasure, glad it worked fine.

As for your question, is in works with CIDRs and individual addresses.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.