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

CodeIgniter项目本地运行遇HTTP Error 404资源未找到求助

Troubleshooting CodeIgniter Local 404 Error (XAMPP)

Hey there, let's work through this 404 issue step by step—since you're new to CodeIgniter, we'll cover all the common pitfalls that cause this when running a friend's project locally with XAMPP.


1. Double-check your project's location in XAMPP

First off, make sure your project is placed correctly in XAMPP's document root:

  • XAMPP stores web-accessible files in the htdocs folder (usually C:\xampp\htdocs on Windows or /Applications/XAMPP/htdocs on Mac).
  • If your project is in a subfolder like htdocs/my_ci_project, your access URL should be http://localhost:8000/my_ci_project/—not just localhost:8000. Skipping the subfolder is a super common first mistake!

2. Verify index.php path configurations

Open your project's root index.php file and confirm these paths match your project structure:

  • For CodeIgniter 3:
    • $system_path = 'system'; (must point to the core CodeIgniter system folder)
    • $application_folder = 'application'; (points to your app's code folder)
  • For CodeIgniter 4:
    • $appPath = 'app'; (points to the main app directory)
    • $systemPath = 'system'; (core system folder)
      Typos or incorrect relative paths here will prevent CodeIgniter from loading its core files, leading straight to a 404.

3. Fix config.php settings

You updated base_url, but let's make sure it's formatted correctly for your setup:

  • For CI3:
    $config['base_url'] = 'http://localhost:8000/my_ci_project/'; // don't forget the trailing slash
    $config['index_page'] = ''; // set to empty to remove index.php from URLs
    $config['uri_protocol'] = 'REQUEST_URI'; // ensures proper URL parsing
    
  • For CI4:
    public $baseURL = 'http://localhost:8000/my_ci_project/';
    public $indexPage = '';
    

4. Get .htaccess and mod_rewrite working

URL rewriting (to remove index.php from URLs) is a frequent culprit for 404s:

  1. Enable mod_rewrite in Apache:
    • Open XAMPP Control Panel → Click Config under Apache → Select httpd.conf
    • Find #LoadModule rewrite_module modules/mod_rewrite.so and remove the leading #
    • Save the file and restart Apache
  2. Use the correct .htaccess for your CI version:
    • Place the file in your project's root (CI3) or the public folder (CI4):
      • CI3 .htaccess:
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
        
      • CI4 .htaccess:
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
        
    • If your project is in a subfolder, adjust the RewriteRule to include the folder name:
      RewriteRule ^(.*)$ /my_ci_project/index.php/$1 [L]
      

5. Check default routing

Make sure your project has a valid default controller set:

  • CI3: Open application/config/routes.php and confirm:
    $route['default_controller'] = 'Welcome'; // matches application/controllers/Welcome.php
    
    Ensure Welcome.php exists and has an index() method.
  • CI4: Open app/Config/Routes.php and check:
    $routes->setDefaultController('Home'); // matches app/Controllers/Home.php
    
    Verify Home.php exists and extends BaseController.

6. Test the base access point

As a quick sanity check, try accessing http://localhost:8000/my_ci_project/index.php directly. If this loads the default page, the issue is with URL rewriting (mod_rewrite or .htaccess). If this also throws a 404, go back and recheck your project path, index.php configurations, and base_url settings.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:13:17