[SOLVED] Blocking countries via htaccess

Hey all,

Silly question I’m sure, but do countries generally get an IP range that can be banned? Say for example China (nothing against China), has a IP that always starts with 40.xxx.xxx.xxx, I’d like to block them via the htaccess file (unless there’s a better way) with wild cards, like 40...* if that’s even possible?

Thanks!

Chris

There are geoIP lists you can get, like the one from maxmind. The More accurate (more frequently updated) ones are paid but there are free lists. I’m not sure if you can do it with htaccess but using a firewall might be a better option anyway. There are no rules that link an IP address range to a country. The lists work by finding out which IP blocks belong to which ISPs and what the location of those ISPs are, this changes all the time hence the need for the lists to be kept up to date.

1 Like

Thank you for that Dexter, really kind of you. I’ll have a look at maxmind, I was fumbling with someones website the other day and found a link to this dating looking, but perhaps informative website. I wonder if that’s a good start - like everyone, I don’t want to pay.

https://www.wizcrafts.net/htaccess-blocklists.html

You said about using a Firewall, I’m going to look into that to see if my hosting offers that.

Really helpful to know that countries aren’t assigned a specific IP address, kind of a shame, but I say that with no experience of connecting countries!

Really useful though, thank you @Dexter_Kane :+1:

Some firewall have this functionality but I suspect it requires paid subscriptions in most cases.

https://knowledgebase.paloaltonetworks.com/KCSArticleDetail?id=kA10g000000ClbHCAS

1 Like

Thank you for that, I checked briefly with my hosting and they don’t have a firewall function. I expect that for little one man businesses, it’s probably not worth it. Oh well!

Why do you want to block an entire country? That seems a bit odd.
Or do you want to block (do something different with) a specific language?

1 Like

It might seem over kill but I only ask for an on-site service web site, like pool cleaners, gutter repair, etc. I thought it might cut down on form spamming and unnecessary usage. I know some might VPN but if they’re prepared to do that, they’re welcome!

2 Likes

That might be nice too, I’m guessing the head code already put in that relates to language helps?

Thanks for your time btw :+1:

You can use a combination of alternate/hreflang and canonical to instruct search engines (e.g. Google, Yandex) to treat one of your pages as the default, and another to be indexed and presented to users with specific languages in specific regions.

<link rel="alternate" href="https://example.com.au/de/" hreflang="de-de" />
<link rel="alternate" href="https://example.com.au/us/" hreflang="en-us" />
<link rel="alternate" href="https://example.com.au/fr/" hreflang="fr" />
<link rel="canonical" href="https://example.com.au/" />

In the above example, line:

  1. applies to German speakers in Germany
  2. applies to English speakers in the USA
  3. applies to all French speakers, everywhere
  4. is the default for everyone else

Put lightweight/static/form-less content on the undesirable language page(s) and that should help a bit. Just make sure you don’t link back to the canonical page from those.

This approach works by “tainting” the search engine results such that spammers can’t ‘find’ your real site in the first place. It can be circumvented by simply changing the language/region settings on your browser/operating system. If your web logs show that the spammers aren’t bothering to do that, however, then it could prove effective.

Note: The example above is a blacklist approach — directing everyone towards your legitimate/fully-functional page except for a specific set of exceptions. You can flip it around and adopt a whitelist approach — misdirecting everyone by default and only directing a subset towards the legitimate page:

<link rel="alternate" href="https://example.ca/local/" hreflang="fr-ca" />
<link rel="canonical" href="https://example.ca/" />
  1. French-speakers in Canada are directed to the fully-featured part of the site
  2. the rest of the world is directed to a limited part of the site

Spammers can be persistent so defence has to be multi-layered. This is just an extra layer that can be used.

2 Likes

Put it behind Cloudflare free, protects you from all sorts of attacks, ddos, and handles geoblocking as well. Hides your IP as well, which is really helpul for security, and works as a free CDN to cut down your transfers/bandwidth costs. It’s an amazing free service that anyone hosting a website with their own domain should use.

2 Likes

That’s brilliant L1, thank you very much, I really do appreciate your full explanation and code, massively kind of you. I’ll definitely do that as it actually makes sense to me! Very kind indeed though, thank you :+1:

1 Like

Thank you for that @Ruffalo , I had heard Cloudflare but never really knew what it did. I’ll definitely look into it, very much appreciated. Oh, it won’t affect SEO will it?

No, it doesn’t change the content of your site.

1 Like

Cheers for confirming mate :+1: