A Helper Script to Easily Block Website Traffic by Country · Jens Oliver Meiert

0
4


.htaccess: A Helper Script to Easily Block Website Traffic by Country

Published on Sep 3, 2025, filed under development, advocacy (feed). (Share this on Mastodon or Bluesky?)

If you run your own website (which, if a web developer peer, I’d say you benefit from), there’s a good chance you run it on Apache.

Whatever the motivation for you to consider this—though after all, there are countries attacking, invading, and occupying other countries (e.g., Russia, Israel) and countries committing genocides (e.g., Israel)—, here’s one streamlined approach to geographic IP blocking via .htaccess:

  1. Pull country IP ranges via IPdeny and generate the respective block list via a bash helper script
  2. Add the script output to your .htaccess and upload the updated file
  3. That’s it

Is that really it? Well, you’d want to keep those lists up-to-date, so you could set up a cron job and/or a reminder to update the lists periodically.

What does that helper script do? It’s straightforward: Pull data from IPdeny for whatever countries you specify, and wrap the data in an .htaccess-ready RequireAll block.

What’s not so straightforward are the performance-related options (the code for these, that is): You can, for example, use --min-prefix 16 only to include large network blocks (like /13, /14, /15, /16), while excluding smaller subnets (like /24, /30, /32). This can reduce thousands of IP ranges down to a couple of hundred, for much better Apache performance.

IPdeny appears to be relatively conservative, so even the default approach shouldn’t over-block. And while it won’t deny all traffic from specified regions, it could serve whatever your motivation—whether that’s compliance, testing, or making a statement.

As one action to take as part of a personal policy, please keep donating to people and nations currently under attack, like Ukraine and Palestine. Thank you.

About Me

I’m Jens (long: Jens Oliver Meiert), and I’m a web developer, manager, and author. I’ve been working as a technical lead and engineering manager for companies you’ve never heard of and companies you use every day, I’m an occasional contributor to web standards (like HTML, CSS, WCAG), and I write and review books for O’Reilly and Frontend Dogma.

I love trying things, not only in web development and engineering management, but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (I value you being critical, interpreting charitably, and giving feedback.)

Source link