You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为使用CodeIgniter Language库的多语言网站配置域名对应语言

嘿,这个需求其实挺常见的,我之前帮朋友处理过类似的多域名绑定多语言的场景,用CodeIgniter实现起来很顺畅,下面给你分步拆解具体操作:

在CodeIgniter中配置多域名对应不同语言

1. 确认基础语言库已就绪

首先确保你已经完成了Language库的基础配置:

  • 已在对应目录放置丹麦语(da)和英语(en)的语言文件:
    • CI3:application/language/da/、application/language/en/
    • CI4:app/Language/da/、app/Language/en/
  • 已在配置文件中启用语言功能(CI3的application/config/config.php、CI4的app/Config/App.php)

2. 封装域名判断逻辑(用基础控制器复用代码)

把域名识别和语言切换的逻辑抽进基础控制器,这样所有业务控制器都能继承复用,不用重复写代码。

针对CodeIgniter 3

在application/core/目录下创建MY_Controller.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        
        // 获取当前访问的域名
        $host = $this->input->server('HTTP_HOST');
        
        // 建立域名与语言的映射关系
        $lang_map = [
            'www.example.dk' => 'da',
            'www.example.en' => 'en'
        ];
        
        // 设置默认兜底语言,防止域名不在列表中的情况
        $default_lang = 'en';
        
        // 确定当前要加载的语言
        $current_lang = isset($lang_map[$host]) ? $lang_map[$host] : $default_lang;
        
        // 配置网站语言并加载对应语言文件
        $this->config->set_item('language', $current_lang);
        $this->lang->load('common', $current_lang); // 替换成你的通用语言文件名
    }
}

之后所有业务控制器都继承MY_Controller,比如:

class Home extends MY_Controller {
    // 你的业务逻辑代码
}

针对CodeIgniter 4

修改或创建app/Controllers/BaseController.php:

<?php namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class BaseController extends Controller {

    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) {
        parent::initController($request, $response, $logger);
        
        // 获取当前访问的域名
        $host = $this->request->getServer('HTTP_HOST');
        
        // 域名-语言映射表
        $lang_map = [
            'www.example.dk' => 'da',
            'www.example.en' => 'en'
        ];
        
        $default_lang = 'en';
        $current_lang = isset($lang_map[$host]) ? $lang_map[$host] : $default_lang;
        
        // 设置语言环境并加载对应文件
        $this->request->setLocale($current_lang);
        $this->lang->load('Common', $current_lang);
    }
}

业务控制器直接继承BaseController即可。

3. 配置服务器域名指向

把www.example.dk和www.example.en都指向你的CodeIgniter项目根目录(包含index.php的目录),下面是两种常见服务器的配置示例:

Apache(.htaccess)

确保项目根目录的.htaccess能正确转发请求:

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

Nginx

添加两个server块指向同一项目目录:

server {
    listen 80;
    server_name www.example.dk;
    root /path/to/your/codeigniter/project;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整路径
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

server {
    listen 80;
    server_name www.example.en;
    root /path/to/your/codeigniter/project;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

4. 测试验证

分别访问www.example.dk和www.example.en,检查页面文本是否自动切换到对应语言。如果有问题,可以先打印$current_lang的值,确认域名是否被正确识别。

另外,如果之前有通过会话/Cookie保存用户手动选择的语言,你可以根据需求调整逻辑:比如优先域名绑定的语言,覆盖用户之前的选择,或者保留用户选择(这种场景较少见)。

内容的提问来源于stack exchange,提问作者akash99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:46:57