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

CodeIgniter多应用部署:将mobile.php改为mobile优化URL

实现CodeIgniter移动端URL从mobile.php改为mobile的方法

我之前也处理过类似的多应用CodeIgniter部署场景,给你分步骤说怎么实现:

1. 调整.htaccess重写规则

首先要修改根目录下的.htaccess,添加针对移动端URL的重写规则,把/mobile/xxx的请求转发到mobile.php/xxx。注意规则顺序很重要,移动端的规则要放在桌面端规则前面,避免被优先匹配。

完整的.htaccess示例如下:

RewriteEngine On
RewriteBase /

# 处理移动端请求:将/mobile/开头的URL转发到mobile.php
RewriteRule ^mobile/(.*)$ mobile.php/$1 [L]

# 桌面端原有规则:移除index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

2. 配置移动端入口文件mobile.php

确保你的mobile.php正确指向移动端的应用目录webmobile,并且基础配置和桌面端的index.php一致,只是替换应用文件夹路径。示例代码如下:

<?php
// 设置环境变量,按需调整
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

// 系统目录路径(根据你的实际位置调整,默认是根目录下的system)
$system_path = 'system';
// 移动端应用目录
$application_folder = 'webmobile';

// 以下是CodeIgniter标准入口文件逻辑,无需修改
if (realpath($system_path) !== FALSE) {
    $system_path = realpath($system_path) . '/';
}
$system_path = rtrim($system_path, '/') . '/';

if (!is_dir($system_path)) {
    exit("系统文件夹路径配置错误,请检查当前文件的路径设置:" . pathinfo(__FILE__, PATHINFO_BASENAME));
}

define('BASEPATH', str_replace("\\", "/", $system_path));
define('APPPATH', $application_folder . '/');
define('VIEWPATH', APPPATH . 'views/');

require_once BASEPATH . 'core/CodeIgniter.php';

3. 修正移动端配置文件的base_url

打开webmobile/config/config.php,把base_url设置为移动端的根URL,这样生成的链接才会正确指向/mobile/开头的路径:

$config['base_url'] = 'http://somewebsite.com/mobile/';

测试验证

完成以上配置后,直接访问http://somewebsite.com/mobile/controller就应该能正常路由到移动端的对应控制器了,和桌面端的http://somewebsite.com/controller访问逻辑一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:38:15