Redirect Visitors by Country for specific URLs?

Mihai B.

Well-known member
Registered
Joined
Apr 19, 2016
Messages
232
Points
18
Hey guys,
I have a website and it has 2 separated sections, one for US visitors and one for CA visitors, I want to create a redirection like this
US visitors visit homepage (example.com) -> no redirect
CA visitors visit homepage (example.com) -> will be redirected to example.com/blog/

How can I set this with my web hosting, htaccess, PHP script or something else to do the job?
 

mobin

Well-known member
Registered
Joined
Jun 22, 2017
Messages
234
Points
28
You can use PHP GeoIP module in your application to decide the redirection. There is htacces trick as well like THIS. But usually I prefer this kind of Geo redirect managed in application code to have better control and logging.
 

Mihai B.

Well-known member
Registered
Joined
Apr 19, 2016
Messages
232
Points
18
You can use PHP GeoIP module in your application to decide the redirection. There is htacces trick as well like THIS.
I read to this line

If you want to redirect based on the country using mod_rewrite in combination with mod_geoip, your .htaccess file could look like this:
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(NL|BE)$
RewriteRule ^(.*)$ http://www.mydomain.com/nl/$1 [L]
What to replace for ENV:GEOIP_COUNTRY_CODE? where to get this?

What is NL|BE in line above?

But usually I prefer this kind of Geo redirect managed in application code to have better control and logging.
What do you mean? It will be a custom code in the web system that we should using?
 

mobin

Well-known member
Registered
Joined
Jun 22, 2017
Messages
234
Points
28
mobin
To get the country code visit - https://dev.maxmind.com/geoip/legacy/codes/iso3166/

For the application level handing, you should write your code to decide the redirection where it will check the source and decide to which URL it should go to. Later this will help you for some extensive logging, any kind of personalisation/language preferences, etc as you wish compared to htaccess redirect.
 

Malcolmjr96

Moderator
Staff Member
Joined
Apr 18, 2016
Messages
433
Points
28
Just checking in.. Have you figured out a solution for this?
 
Recommended Threads

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