I’m trying to clean up my URIs on a multi-language CI site by changing the segment containing the language name to just the two-character language code.
Currently, my URIs look like this:
http://example.com // Home (English)
http://example.com/english/home // Home (English)
http://example.com/home // 404 (should go to english/home)
http://example.com/sv // 404 (should go to swedish/home)
http://example.com/swedish/home // Home (Swedish)
http://example.com/sv/home // 404 (should go to swedish/home)
I have experimented both with application/config/routes.php and .htaccess, but I feel I’m not making much progress. Which should I be using? How can I achieve my desired results?
As it stands, my files look like this:
// .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/index.php/$1 [L]
// application/config/routes.php (minus docs)
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "pages/view/home";
$route['([a-z]+)/([a-z]+)'] = "pages/view/$2";
?>
// application/controllers/page
<?php
class Pages extends CI_Controller {
public function __construct() {
parent::__construct();
$this->language = $this->uri->segment(1);
}
public function view ($page = 'home') {
if (!file_exists('application/views/pages/'.$page.'.php'))
show_404();
$data['title'] = $page;
$this->lang->load('general',$this->language);
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
Try this in routes.php:
Controller constructor:
View function: