国际化的URI与CI路由或mod_rewrite的路由、URI、CI、mod_rewrite

2023-09-02 10:10:07 作者:最是奢侈少年梦

我试图通过改变包含语言的名字,只是两个字符的语言code段清理我的URI在一个多语言CI网站。

目前,我的URI是这样的:

  http://example.com //首页(英文)
http://example.com/english/home //首页(英文)
http://example.com/home // 404(应该去英语/家)

http://example.com/sv // 404(应该去瑞典/家)
http://example.com/swedish/home //首页(瑞典)
http://example.com/sv/home // 404(应该去瑞典/家)
 

我已经和这两个实验应用程序/配置/ routes.php文件的.htaccess ,但我觉得我'米赚不了多少进展。其中我应该使用?我怎样才能达到我预期的效果?

既然这样,我的文件是这样的:

  //的.htaccess
RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。*)$ /example/index.php/$1 [L]

//应用程序/配置/ routes.php文件(减去文档)
< PHP
如果(!定义(BASEPATH'))出口('没有直接的脚本允许访问);
$航线['default_controller'] =页/视图/家;
$路线[([A-Z] +)/([A-Z] +)'] =页/图/ $ 2;
?>

//应用程序/控制器/页
< PHP

类网页扩展是CI_Controller {

    公共职能__construct(){
        父:: __结构();
        $这 - >语言= $这 - > URI的>段(1);
    }

    公共职能视图($页='家'){

        如果(!file_exists(应用程序/视图/页/'.$页面。'PHP'))
            show_404();

        $数据['标题'] = $页;

        $这个 - >朗GT&;负载('将军',$这个 - >语言);
        $这个 - >负载>查看('模板/头,$数据);
        $这个 - >负载>查看(页面/'.$页面,$数据);
        $这个 - >负载>查看('模板/页脚,$数据);

    }
}
?>
 
Nginx Rewrite

解决方案

在routes.php文件试试这个:

  $航线['default_controller'] =页/视图/家;
$航线['(EN | SV)] ='页/视图/家;
$航线['(EN | SV)/([A-Z] +)] ='页/视图/ $ 2';
 

控制器构造:

 函数__construct(){
    父:: __结构();
    $这个 - > lang_array =阵列('恩'=>英语,SV=>'瑞典');
    $这个 - > current_lang = $这个 - > URI的>段(1,'恩');
}
 

查看功能:

 公共职能视图($页='家'){
            $ lang_folder = $这个 - > lang_array [$这个 - > current_lang]。
            // $ lang_folder将是'英语'的默认
            // 其他的东西
            $这个 - >朗GT&;负载('将军',$ lang_folder);

}
 

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:

$route['default_controller'] = "pages/view/home";
$route['(en|sv)'] = 'pages/view/home';
$route['(en|sv)/([a-z]+)'] = 'pages/view/$2';

Controller constructor:

function __construct(){
    parent::__construct();
    $this->lang_array = array('en' => 'english', 'sv' => 'swedish');
    $this->current_lang = $this->uri->segment(1, 'en');
}

View function:

public function view ($page = 'home') {
            $lang_folder = $this->lang_array[$this->current_lang];
            // $lang_folder will be 'english' by default
            // other stuff
            $this->lang->load('general', $lang_folder);

}