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

CodeIgniter base_url配置问题:自定义域名访问404但localhost正常

问题分析与解决步骤

你的问题核心是自定义域名Office-Inventory没有被正确映射到CodeIgniter项目,而localhost:8000能正常工作是因为它直接指向了项目入口。以下是具体排查和解决步骤:

1. 本地DNS解析配置

自定义域名需要先在本地hosts文件中映射到本机IP,否则系统无法找到这个域名对应的服务器:

  • Windows系统:打开C:\Windows\System32\drivers\etc\hosts文件(需管理员权限),添加两行:
    127.0.0.1 Office-Inventory
    ::1 Office-Inventory
    
  • 保存文件后,打开命令行执行ping Office-Inventory,确认返回的是127.0.0.1或::1。

2. Apache虚拟主机配置

Apache默认不会自动识别自定义域名,需要配置虚拟主机指向你的CodeIgniter项目:

  1. 打开Apache的httpd.conf文件,确保以下行没有被注释(去掉前面的#):
    LoadModule vhost_alias_module modules/mod_vhost_alias.so
    Include conf/extra/httpd-vhosts.conf
    
  2. 打开conf/extra/httpd-vhosts.conf文件,添加虚拟主机配置:
    • 如果是CodeIgniter 3:
      <VirtualHost *:80>
          ServerName Office-Inventory
          DocumentRoot "D:/path/to/your/codeigniter-project"
          <Directory "D:/path/to/your/codeigniter-project">
              AllowOverride All
              Require all granted
          </Directory>
      </VirtualHost>
      
    • 如果是CodeIgniter 4:
      <VirtualHost *:80>
          ServerName Office-Inventory
          DocumentRoot "D:/path/to/your/codeigniter-project/public"
          <Directory "D:/path/to/your/codeigniter-project/public">
              AllowOverride All
              Require all granted
          </Directory>
      </VirtualHost>
      
    注意替换D:/path/to/your/codeigniter-project为你的实际项目路径。
  3. 重启Apache服务,使配置生效。

3. 检查CodeIgniter的base_url配置

确保配置和虚拟主机的ServerName完全一致:

  • CodeIgniter 3:打开application/config/config.php,确认:
    $config['base_url'] = 'http://Office-Inventory/';
    
  • CodeIgniter 4:除了app/Config/App.php中的public $baseURL = 'http://Office-Inventory/';,还要检查项目根目录的.env文件,如果里面有app.baseURL配置,要确保和上面一致(.env的配置会覆盖Config文件)。

4. 验证.htaccess配置

确保项目的重写规则正确,让Apache能把请求转发到CodeIgniter的入口文件:

  • CodeIgniter 3:项目根目录的.htaccess内容应为:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
  • CodeIgniter 4:public目录下的.htaccess内容应为:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    同时确保Apache的mod_rewrite模块已启用(httpd.conf中LoadModule rewrite_module modules/mod_rewrite.so没有被注释)。

5. 端口冲突排查

错误信息中显示Server at dict-inventory Port 80,确认Apache确实在80端口运行,没有被其他程序(如IIS、迅雷等)占用。可以在命令行执行netstat -ano | findstr ":80"查看占用情况,如有冲突关闭占用程序或修改Apache监听端口。

完成以上步骤后,重新访问http://Office-Inventory/应该就能正常加载项目了。

内容的提问来源于stack exchange,提问作者Vic Zoren Abucot

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 14:12:35