Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 带CI路由或mod_重写的i18n URI_.htaccess_Codeigniter_Url Routing - Fatal编程技术网

.htaccess 带CI路由或mod_重写的i18n URI

.htaccess 带CI路由或mod_重写的i18n URI,.htaccess,codeigniter,url-routing,.htaccess,Codeigniter,Url Routing,我试图通过将包含语言名称的段更改为仅包含两个字符的语言代码来清理多语言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.co

我试图通过将包含语言名称的段更改为仅包含两个字符的语言代码来清理多语言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)
// .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);

    }
}
?>
我已经尝试了
application/config/routes.php
.htaccess
,但我觉得我没有取得多大进展。我应该用哪一种?我怎样才能达到我想要的结果

目前,我的文件如下所示:

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)
// .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);

    }
}
?>
/.htaccess
重新启动发动机
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$/example/index.php/$1[L]
//application/config/routes.php(减去文档)
//应用程序/控制器/页面

在当前规则之上,添加以下内容:

# You already had this one
RewriteEngine On

RewriteRule ^home(.*)$ /english/home$1 [L]
RewriteRule ^sv(/home)?(.*)$ /swedish/home$1 [L]
然后是您已经拥有的:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /kappahl-hilys/index.php/$1 [L]

在routes.php中尝试以下操作:

$route['default_controller'] = "pages/view/home";
$route['(en|sv)'] = 'pages/view/home';
$route['(en|sv)/([a-z]+)'] = 'pages/view/$2';
控制器构造函数:

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

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);

}
您还可以:
在第一个Hook(pre_system)文件中,检测URI中的语言:

    <?php

   class Lang_uri {

 public function run() {

    $languages = array('fr', 'en');

    //try to find language in $_SERVER['REQUEST_URI']
    foreach($languages as $lang)
    {
        if(strpos($_SERVER['REQUEST_URI'], '/'.$lang)===0)
        {
            //Store language founded
            define('URI_LANG', $lang);

            //Delete it, codeigniter will don't know is exists !
            $_SERVER['REQUEST_URI'] = substr_replace($_SERVER['REQUEST_URI'], '', strpos($_SERVER['REQUEST_URI'], '/'.$lang)+1, strlen($lang)+1);

            break;
        }
    }

    if(!defined('URI_LANG'))
        define('URI_LANG', 'fr');
}
    }
就这些。
并且也会起同样的作用。
有关CI钩子的更多信息:

对不起,我的英语很差。

啊,我有几件事我应该说得更清楚:1)
example.com/home
也可以是
example.com/about
;这是一个可变的页面名称。它还必须与
example.com/sv/about
一起使用。2) 我忘了提到语言代码不是真正的路径。语言段是在我的
页面中创建的
类。我将在我的问题中添加该代码。这几乎有效。现在,CI正在以各自的语言代码命名的文件夹中查找语言文件,而语言文件实际上存在于以各自的语言名称命名的文件夹中。基本上,您已经使URI看起来像它应该的样子,但是我如何路由/sv/to/swedish/(对于其他语言也是如此)?这就是我应该使用
.htaccess
的原因吗?我已经更新了上面的代码。使用关联数组将语言的缩写(en)与其全名(英语)链接起来,您可以同时访问这两种语言。这有帮助吗?它在同一行抛出
消息:未定义属性:页面::$current\u lang
未定义索引:
$this->current\u lang=$this->uri->segment(1,'en')以前工作过,但现在抛出消息?您复制了我更新的构造函数代码和视图函数了吗?第14行
$this->language=$this->uri->segment(1,'en')需要是
$this->current_lang=$this->uri->段(1,'en')