To limit access based on certain file, use the following:
Example:
<Files wp-login.php>
order deny,allow
deny from all
allow from 139.82.0.0/16
</Files>
To be able to use geolocation and redirect a client based on country use mod_geoip2 Apache module
:
Example:
GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat
# Redirect one country
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^(.*)$ http://www.canada.com$1 [R,L]
# Redirect multiple countries to a single page
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|US|MX)$
RewriteRule ^(.*)$ http://www.northamerica.com$1 [R,L]
Another solution is to manually insert a list of IPs that would be blocked. There is online service called HTACCESS Block Country by IP Range that would generate for you a list of IPs to block.
Example:
# BLOCK COUNTRY BY IP RANGE
# IncrediBILL's HTACCESS Tools
# http://incredibill.me
<Limit GET POST HEAD>
order allow,deny
#
# Block from BRAZIL (BR)
#
deny from 139.82.0.0/16
deny from 143.54.0.0/16
deny from 143.106.0.0/16
deny from 143.107.0.0/16
deny from 143.108.0.0/16
deny from 146.134.0.0/16
deny from 146.164.0.0/16
deny from 147.65.0.0/16
deny from 150.161.0.0/16
deny from 150.162.0.0/16
deny from 150.163.0.0/16
One more possible and most appropriate solution for you, would be using PHP geoip module
, because you want to limit a user an access to certain parts of the page and doing it using PHP is much better.
First of all, install php-pecl-geoip module for PHP. Depending on your server configuration, installation is usually straight-forward. If you are using RedHat/CentOS simply run: yum install php-pecl-geoip
. You must have root access to your server to install software, if you are using shared hosting, ask your administrator to do it for you.
After installation, you will have a whole new set of PHP functions available for you. The one you should use at the moment is geoip_continent_code_by_name()
, as you asked to block all users from South America.
Applying it to your example is easy:
if ( geoip_continent_code_by_name( $_SERVER['REMOTE_ADDR'] ) == 'SA' ) {
echo '<a href="#">YouTube Video Example: This video is not available for South America.</a>';
} else {
echo '<a href="http://www.youtube.com/watch?v=videoCode">YouTube Video Example</a>';
}
To test it on you server, you could just replace $_SERVER['REMOTE_ADDR']
with one of the IPs that belong to Brazil, e.g.: geoip_continent_code_by_name('146.164.22.1');