Can I use a service worker to deploy an htaccess rule?

Thanks in advance. Can I use a service worker to deploy the following htaccess declaration?

RewriteCond %{HTTP:CF-IPCountry} ^$ RewriteRule ^ - [F,L] allow from

You can do a similar behavior with a worker, but you have to write it in javascript.

1 Like

What are you trying to do with that declaration? It almost looks like something you can do with Firewall Rules.

Write it in javascript, right. I tried using an online htaccess-to-nginx converter, but I’m not accomplished enough to interpret the results! Then I figured before I try to write a worker, I should ask if it is even possible.

sdayman – what I am trying to do is use Cloudflare to implement the advice you gave from another post. You suggested using htaccess to prevent bypassing cloudflare (when one has Cloufflare GeoIP enabled), the rule I listed above. Your advice works great! But only when Cloudflare is enabled. When I disable Cloudflare, I can’t access my site.

A Worker requires that Cloudflare is enabled for that hostname. And putting that in a worker doesn’t block requests that bypass Cloudflare.

That’s the point. It stops anybody from going direct to origin. So if there’s a time you need to briefly bypass Cloudflare, comment out that line in .htaccess.

Thanks for helping. “Comment out that line in htaccess,” yes I’ve done that. Just thought a service worker would mean I would not have to alter the htaccess.

Nope. (I’m just reiterating to be more clear).

You need to toggle that block setting at your host’s end. Either .htaccess like you’re using, or server firewall like I’m using. I know it’s a bit of a hassle, but I bypass Cloudflare so rarely these days it’s not even a bother for the very few times I do it. Ok, it’s a bit of a bother…albeit rare.


1 Like