简体   繁体   中英

ip address redirect based on countries

Not sure if this can be done.
But I need a way to redirect visitors based on their country IP address.

Before anyone mentions it,
yes I know about Geoip , but my host does not have it, and i do not have shell access.
So Im looking for another option.

Here is the problem,
My website is generally for personal use, and I am being bombarded by spammers and unwanted crawlers plus
people looking for files i do not even have.these are from visitors all over the globe.
so i need a way to do this efficiently.

Right now im just using a messy way with regex to redirect a few of them.
like this.

#china ips-
#
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^(49\.(4|51|52|64|239)|54\.222) [OR]
RewriteCond %{REMOTE_ADDR} ^(91\.234|134\.196|159\.226|161\.207|166\.111) [OR]
RewriteCond %{REMOTE_ADDR} ^(168\.160|192\.(124|188)|193\.0)
RewriteRule !^folder/country\.html http://www.website.com/folder/country.html [L,NC,R]

While this works, it only redirects the listed regex ip addresses to a page i select.
It is good for maybe 20+ ip addresses, but if you have more than 1000+ ip addresses you need to redirect,
(which i do) your htaccess file tends to get very very big and cluttered up.

I would rather have the ip addresses in a text file, and have htaccess look for it in the cn.txt ,
or china.txt then somehow redirect any ip addresses it finds in that txt file to
http://www.website.com/folder/country.html
I know if my host had geoip it would probably make this alot easier.

I forgot to mention, my website is about 85% html, while i do have a forum gallery and faq that are php,
i generally use mostly html code. thanks

You would need to add the following PHP script to all your PHP scripts that a visitor can open. Many PHP applications have a bootstrap script where you can put this code. However, if it is not your case then htaccess is a better choice.

index.php script:

<?php
// Get user's IP address
$remoteAddr = $_SERVER['REMOTE_ADDR'];
// Load a list of regular expressions that redirect the user to 'http://www.website.com/folder/country.html'
$regexes = explode("\n", file_get_contents(dirname(__FILE__) . "/cn.txt"));

foreach ($regexes as $regex) {

    if (preg_match('/^' . $regex . '$/', $remoteAddr)) {             
         header("Location: http://www.website.com/folder/country.html");
         exit;
    }
}

Contents of cn.txt:

49\.(4|51|52|64|239)|54\.222
91\.234|134\.196|159\.226|161\.207|166\.111
and so on

Note: You can also generate your htaccess by a script (in a language of your choice). I mean you can have a list of regular expressions and generate your htaccess from that list every time you modify the list.

There are redirection solutions that doesn't require any code or any db in your server. One of them is www.redirectbycountry.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM