我试图通过将包含语言名称的段更改为仅两个字符的语言代码来清理多语言 CI 站点上的 URI。
目前,我的 URI 如下所示:
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)
我已经尝试过application/config/routes.php
和.htaccess
,但我觉得我没有取得太大进展。我应该使用哪个?我怎样才能达到我想要的结果?
就目前而言,我的文件如下所示:
// .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);
}
}
?>