I have a site with multiple languages. For my news page I have two rules to route the pagination variable to my controller. One for all the languages (en, ct, cs, kr), and one for the default language.
Routes.php
$route['^(en|ct|cs|kr)/news/page/(:num)'] = 'news/index/$1';
$route['news/page/(:num)'] = 'news/index/$1';
News controller
public function index($id)
{
echo $id;
}
The routes are accessing the news controller, however the $id parameter isn’t being passed to the index() method.
If I echo the $id it returns the language segment rather than the pagination variable I am expecting:
mysite.com/en/news/page/2 // $id returns ‘en’.
mysite.com/kr/news/page/2 // $id returns ‘kr’.
It works when I write the routes out individually for each language:
$route[‘en/news/page/(:num)’] = ‘news/index/$1’;
Am I going wrong somewhere with my regex?
That’s because in your first rule, you’re capturing 2 segments of the URL. The first one is the language (e.g.
en), and the second one is theid(or page number). So, in your first rule you should instead use$2instead of$1.