Allow IP addresses dynamically using .htaccess or php?

Joined
Apr 15, 2016
Messages
60
Best answers
0
Ratings
3
Points
8
#1
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

Global Mod
Staff Member
Joined
May 3, 2016
Messages
1,551
Best answers
4
Ratings
412 13
Points
83
#2
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
Joined
Jun 22, 2017
Messages
212
Best answers
0
Ratings
82 1
Points
28
#3
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.
 
Joined
Apr 15, 2016
Messages
60
Best answers
0
Ratings
3
Points
8
#4
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
 
Joined
Jan 19, 2013
Messages
33
Best answers
1
Ratings
9
Points
8
#5
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
Joined
Feb 19, 2013
Messages
299
Best answers
2
Ratings
25
Points
28
#6
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
Recommended Threads
Replies
9
Views
143
Replies
13
Views
1,564
fwh
Replies
7
Views
519
Replies
6
Views
2,762

Latest postsNew threads

Latest Hosting OffersNew Reviews

Sponsors

Latest Blog ArticlesMost Viewed Threads

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