How to allow access only within country

Tag: .htaccess Author: dailaxuying Date: 2009-08-03

I found this web site to generate a .htaccess to block an access from certain country.

The problem with this is that I want to allow access only within Norway. If I use this service, the list will be very long since I have to list all the country IP addresses.

Is there any way to allow access within country, my case is Norway?

I'm probably wrong (correct me if I am), but wouldn't it work, if you would swap those values from generated .htaccess from order allow,deny to order deny,allow, then replace each deny to allow (to allow all ip's within 1 country) and then, at the end, put deny from all?
ad. From gbjbaanb's post I see, that I was very close to the real answer ;).

Best Answer

Change all occurences of 'deny' to allow, and all occurrences of 'allow' to deny. Then move the 'deny from all' condition at the end to the beginning of the list.


order allow,deny
deny from
deny from .... 
allow from all


order deny,allow
deny from all
allow from
allow from ....

There's some good tutorials about .htaccess.


do you know how you would do this for just a certain page?
yes. Google says to use the <files> block within the .htaccess file. - note that all .htaccess directives can be put in your httpd.conf instead (and is preferred)

Other Answer1

There are many geoip database vendors that offer solutions for the problem detailed instructions on the subject. Check out for an apache module that comes with their database which would probably be a perfect fit for your problem.