-1

I have an ip list file for my country, as like this (txt document):

45.123.116.0/22
5.2.80.0/21
5.11.128.0/17
5.23.120.0/21
5.24.0.0/14
etc

i have two question about that.

1- can i forward the user, if he is in that list via .htaccess file? (if he is, use this adress.. if not this adress)

2- how can i check 'if the user is in my country' via PHP? i mean, how can i say something like that..

if (strstr('list.txt',$_SERVER['REMOTE_ADDR']))
orhuncan
  • 11
  • 4
  • You can't do this using `.htaccess`. You need to check it using php and then redirect to new address. – Mohammad Oct 12 '16 at 14:48

2 Answers2

1

*1).htaccess file

The visitor blocking facilities offered by the Apache Web Server enable us to deny access to specific visitors, or allow access to specific visitors. This is extremely useful for blocking unwanted visitors, or to only allow the web site owner access to certain sections of the web site, such as an administration*

ErrorDocument 403 /specific_page.html
area.*
order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all

When using the "Order Allow,Deny" directive the requests must match either Allow or Deny, if neither is met, the request is denied.

doc 1)http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order

doc 2)http://www.htaccess-guide.com/deny-visitors-by-ip-address/

2) Proof of Concept (can't say this works as is....)

$current_ip = $_SERVER['REMOTE_ADDR'];

 $valid_ip = false;

 // Convert IPs to Regex
 foreach($cfg['ipallowed'] as $index=>$ip);
 {
    $ip = str_replace('.', '\\.', $ip);
    $ip = str_replace('*', '[0-9]|^1?\\d\\d$|2[0-4]\\d|25[0-5]');
    if (preg_match($ip, $current_ip)
    {
      $valid_up = true;
      break;
    }
 }

 if ($valid_ip)
Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43
0

1; you can redirect by IP to a holding page:

# Redirect a user to /specific_page.html based on their IP address.
RewriteCond %{REMOTE_ADDR} ^10\.0\.0\.2$ [OR]
RewriteCond %{REMOTE_ADDR} ^127\.0\.0\.2$
RewriteCond %{REQUEST_URI} !specific_page\.html$
RewriteCond %{REQUEST_URI} !\.(js|png|gif|jpg|css)$
RewriteRule ^ /specific_page.html [R=302,L]

2; see this question/answer which recommends using http://www.hostip.info/use.html.

Community
  • 1
  • 1
Egg
  • 1,782
  • 1
  • 12
  • 28
  • It work for static ip and you can't use list of ip in file. – Mohammad Oct 12 '16 at 14:53
  • Sorry, hadn't seen that. If a list in a file, you'll have to use PHP in your global/header/bootstrap file to check and redirect. If you have a list, why not put that list in htaccess instead? – Egg Oct 12 '16 at 14:55
  • Can you show how can i read list of ip from file and insert into .htaccess? – Mohammad Oct 12 '16 at 14:57
  • As in my answer above; `RewriteCond %{REMOTE_ADDR} ^127\.0\.0\.1$ [OR]`. – Egg Oct 12 '16 at 14:58
  • That is two ip. How you want to insert content of `list.txt` file into htaccess? – Mohammad Oct 12 '16 at 15:00
  • You would add another line for each IP. More can be found in the [Apache docs](http://httpd.apache.org/docs/current/mod/mod_rewrite.html). – Egg Oct 12 '16 at 15:23