Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to block all non UK visitors to my website, does anyone know how I would achieve this?

I think I need to have a .htaccess file with all the IPs listed next to a deny command but how do I find an upto date list of UK IPs to allow.

The reason for this is to try and reduce the amount of spam on my website.

Thanks for your help

share|improve this question
up vote 9 down vote accepted

Have a look at the GeoIP databases.

But i think to block ALL non UK visitors is not the best way. Imagine one of your users is in holidays autside the UK and want to reach your site.

Have a look at the DROP list from Spamhaus. It contains all Spammer owned networks. I use the list within my firewall rules, and reduced the SPAM about 98% on the hosted blogs.

share|improve this answer

Two solutions comes to my mind.

If your website is dynamic you can check en each request the country associated with the visitor ip using for example Maxmind GeoIP Database ( if the user is not from UK just show a 404 or whatever you want.

The other solution is only allow users within a certain ip range access the website. You have to obtain the ip ranges for uk and then add them to the allow directive on the .htaccess file. The ranges can be obtained here (

share|improve this answer

What you want to do is typically called geolocation.

This question on StackOverflow has some useful answers.

share|improve this answer

Not sure that geolocation is a complete solution - nor that you shoul block access using .htaccess

That someone is publishing spam on your system implies that you've got some custom code running there - and AFAIK there's no easy way to do efficient lookups direct from the .htaccess file.

Certainly using geolocation may help - but you only need to control access to the bit of functionality which allows publishing of content. There's a free database you can run a local copy of available here.

If it were me, I'd use this as part of a scoring system, also incorporating

1) self-moderated posts - require user to provide an email address - when they post, don't publish until they click on a link you email to them

2) admin moderation - maybe you don't have time to moderate every post - but what about checking the first N posts from a particular user?

3) use spamassassin to identify spamming posts (NB the more spam posts you can capture (but obviously not publish) the better this will work)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.