Allow IP addresses dynamically using .htaccess or php?

Sean101

Member
Registered
Joined
Apr 15, 2016
Messages
60
Points
8
I have a website and now I want to create a new section on it but this section will be targeted on a specific country. Mean that when people visit it from a country that allowed then they will be redirected to that the section I wanted. My question is, How to allow IP addresses dynamically using .htaccess or php? which is better?
 

VirtuBox

Well-known member
Registered
Joined
May 3, 2016
Messages
1,622
Points
83
I have a website and now I want to create a new section on it but this section will be targeted on a specific country. Mean that when people visit it from a country that allowed then they will be redirected to that the section I wanted. My question is, How to allow IP addresses dynamically using .htaccess or php? which is better?
Hello, to be able to use .htaccess, you will have to install and configure mod_geoip for apache, to be able to identify the country of your visitors.
You can do exactly the same with php, but it will probably loading faster if the redirection is handled by your web server.
 

mobin

Well-known member
Registered
Joined
Jun 22, 2017
Messages
234
Points
28
I suggest to let your application handle it [ PHP level ] in case you want to track anything related to this visit/redirection. You will also get a versatility to handle any additional things [ like showing personalized contents or specific other things ] from application level later on.
 

Sean101

Member
Registered
Joined
Apr 15, 2016
Messages
60
Points
8
Hello, to be able to use .htaccess, you will have to install and configure mod_geoip for apache, to be able to identify the country of your visitors.
Where to install and activate mod_geoip? Does it help me to setup somethings like this
If US audience, they will visit example.com by default on Web CMS 1
If UK audience, they will visit example.com/cms2/ by default, cms2 is built on Web CMS 2

You can do exactly the same with php, but it will probably loading faster if the redirection is handled by your web server.
I still use normal PHP redirection code?
i.e
PHP:
<?php
// PHP permanent URL redirection
header("Location: http://www.example.com/cms2/", true, 301);
exit();
?>
I suggest to let your application handle it [ PHP level ] in case you want to track anything related to this visit/redirection. You will also get a versatility to handle any additional things [ like showing personalized contents or specific other things ] from application level later on.
I am tending to use PHP codes because I see it is flexible to use redirections than .htaccess
 

HostechSupport

Active member
Registered
Joined
Jan 19, 2013
Messages
68
Points
8
Such a rule can be used in htaccess RewriteEngine On
Code:
RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^NG$
RewriteRule ^(.*)$ http://www.google.com [L]
 

BillEssley

Well-known member
Registered
Joined
Feb 19, 2013
Messages
358
Points
28
I would recommend using PHP codes to check IP addresses and redirections instead of using htaccess. This will not consume server resource like using htaccess.
 
Older Threads
Replies
9
Views
3,881
Replies
5
Views
3,515
Replies
19
Views
6,136
Latest Threads
Recommended Threads
Replies
4
Views
2,178
Replies
4
Views
1,445
Replies
50
Views
18,711
Replies
15
Views
6,122

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top