Search code examples
phpcodeignitercodeigniter-2codeigniter-routing

Remove unwanted URL segments in Codeigniter site_url


On the page whose URL is "abc.com/en/login/create_member" using site_url('login') produces a link to "abc.com/en/login/create_member", but the desired URL is "abc.com/en/login/"

I've included the link code and an excerpt from my routes.php file

Thanks in advance for any help on this.

Code:

<a class="link_grad_button" href="<?php site_url('login'); ?>">Login</a></div>

Route Excerpt:

$route[$prepended_lang.'login/(:any)'] = 'login/$1';
// URI like '/en/about' -> use controller 'about'
$route['^(en|br)/(.+)$'] = "$2";
$route['default_controller'] = 'landing';
// '/en', '/de', '/fr' and '/nl' URIs -> use default controller
$route['^(en|br)$'] = $route['default_controller']; 

Solution

  • Either you add language on the link like

    <a class="link_grad_button" href="<?php site_url('en/login'); ?>">Login</a></div>
    

    So, your current expression on your route.php works. i.e.

    $route[$prepended_lang.'login/(:any)'] = 'login/$1';
    

    OR

    Simply, remove $prepended_lang. from your route.php expression like:

    $route['login/(:any)'] = 'login/$1';
    

    This above expression works only when you have a function parameter like

    site_url('login/index') 
    

    where index works as parameter for (:any), so in case your url is site_url('login') only, you have to add:

    $route['login'] = 'login';
    

    before the above expression.

    Actually these two expressions are not required on route.php but because of your appended language, these two expressions seems mandatory.