0

My question is how to append country name in codeigniter url?

Lets say my website is http://mywebsite.com and I am opening the website from Canada so my url should be http://mywebsite.com/canada. Meaning I just want to append the country name in the url, nothing change except this. I have read about routes of codeigniter, I have googled it but all in vain.

If anyone have a clue how to do this please let me know.

talonmies
  • 70,661
  • 34
  • 192
  • 269
  • What is the purpose of doing this unless you are serving separate content for different countries? You should explain a bit more. – kittycat Apr 22 '13 at 00:20

2 Answers2

0

I guess you'd do that with a IP lookup to guess the country based on the request IP address ($_SERVER['REMOTE_ADDR']). Mapping the IP address to a location is pretty well documented. Once you have the country, redirect to the correct controller method.

That what you're looking for?

Community
  • 1
  • 1
John Corry
  • 1,567
  • 12
  • 15
0

This is like for language code in url.

With my solution you can put any segment in your uri and hide it to CI.

http://site.com/page.html will be equal to http://site.com/canada/page.html and vice versa.

set_country_uri.php

//App location
$ci_directory = '/ci/';

//countries
$countries = array('canada','france');

//default country
$country = 'canada';

foreach( $countries as $c )
{
    if(strpos($_SERVER['REQUEST_URI'], $ci_directory.$c)===0)
    {
        //Store country founded
        $country = $c;

        //Delete country from URI, codeigniter will don't know is exists !
        $_SERVER['REQUEST_URI'] = substr_replace($_SERVER['REQUEST_URI'], '', strpos($_SERVER['REQUEST_URI'], '/'.$c)+1, strlen($c)+1);

        break;
    }
}

//Add the country in URI, for site_url() function
$assign_to_config['index_page'] = $country."/";

//store country founded, to be able access it in your app
define('COUNTRY', $country);

index.php

<?php

require('set_country_uri.php');

//ci code ...

So in your CI code use COUNTRY constant to know wich is used. And make your code like routing without take in consideration the country in the uri.

Aurel
  • 3,682
  • 1
  • 19
  • 22