.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:
- Pull country IP ranges via IPdeny and generate the respective block list via a bash helper script
- Add the script output to your .htaccess and upload the updated file
- 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.)
- Kiambu Web Design Services
- Kiambu SEO Services
- Kiambu Digital Marketing Services
- Kiambu Social Media Marketing Services
- Kiambu Lipa Pole Pole Phones
- Nyali Web Design Services
- Nyali SEO Services
- Nyali Lipa Pole Pole Phones
- Mombasa Lipa Pole Pole Phones
- Meru Web Design Services
- Meru SEO Services
- Meru Digital Marketing Services
- Meru Social Media Marketing Services
- CBD Nairobi Web Design Services
- Westlands Web Design Services
- Outer Ring Road Web Design Services
- Outer Ring Road SEO Services
- Thika Road Web Design Services
- Thika Road SEO Services
- Thika Road Digital Marketing Services
- Thika Road Lipa Pole Pole Phones
- Langata Web Design Services
- Langata SEO Services
- Langata Lipa Pole Pole Phones
- Mombasa Road Web Design Services
- Mombasa Road SEO Services
- Mombasa Road Lipa Pole Pole Phones
- Mombasa Road Digital Marketing Services
- Mombasa Road Social Media Marketing Services
- Karen Web Design Services
- Karen SEO Services
- Karen Digital Marketing Services
- Garden City Web Design Services
- Thika Road Mall Web Design Services
- Thika Road Mall SEO Services
- Thika Road Mall Lipa Pole Pole Phones
- Eastlands Web Design Services
- Eastlands SEO Services
- Eastlands Lipa Pole Pole Phones
- Donholm Web Design Services
- Donholm SEO Services
- Donholm Lipa Pole Pole Phones
- Ruai Web Design Services
- Ruai SEO Services
- Ruai Lipa Pole Phones