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

如何在同一服务器域名下部署两个CodeIgniter 3.11应用(根目录+子目录)

解决方案:同一域名下部署CodeIgniter 3根目录与子目录应用

一、修复Apache VirtualHost配置

你的VirtualHost配置存在路径冲突:DocumentRoot /var/www 与 Alias / /var/www/html 会导致根目录访问权限异常(Forbidden),正确配置应直接将DocumentRoot指向site1目录,再单独配置site2的别名:

<VirtualHost *:80>
    ServerName example.com
    # 直接将根目录指向site1的文件夹
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    # 配置site2的别名,指向其实际目录
    Alias /site2 /var/www/site2
    <Directory /var/www/site2>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
        # 确保Apache处理该目录的PHP文件
        php_flag engine on
    </Directory>
</VirtualHost>

说明:删除多余的set DocumentRoot和Alias /,避免根目录路径冲突。

二、调整两个应用的.htaccess文件

1. site1(根目录/var/www/html)的.htaccess

添加规则排除/site2路径,防止site2的请求被错误重写到site1的index.php:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On

    # 跳过site2路径的重写规则
    RewriteRule ^site2/ - [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

2. site2(/var/www/site2)的.htaccess

确保RewriteBase正确适配子目录:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /site2/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

三、配置CodeIgniter应用参数

1. site1的application/config/config.php

$config['base_url'] = 'http://example.com/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

2. site2的application/config/config.php

$config['base_url'] = 'http://example.com/site2/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

说明:设置uri_protocol为REQUEST_URI可避免子目录下的URI解析错误。

四、验证与重启

  1. 重启Apache服务:sudo systemctl restart apache2
  2. 访问http://example.com确认site1正常运行
  3. 访问http://example.com/site2确认site2正常加载

内容的提问来源于stack exchange,提问作者Aravinda B N

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 04:32:59