CodeIgniter项目本地运行遇HTTP Error 404资源未找到求助
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
htdocsfolder (usuallyC:\xampp\htdocson Windows or/Applications/XAMPP/htdocson Mac). - If your project is in a subfolder like
htdocs/my_ci_project, your access URL should behttp://localhost:8000/my_ci_project/—not justlocalhost: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:
- Enable mod_rewrite in Apache:
- Open XAMPP Control Panel → Click
Configunder Apache → Selecthttpd.conf - Find
#LoadModule rewrite_module modules/mod_rewrite.soand remove the leading# - Save the file and restart Apache
- Open XAMPP Control Panel → Click
- Use the correct
.htaccessfor your CI version:- Place the file in your project's root (CI3) or the
publicfolder (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]
- CI3 .htaccess:
- If your project is in a subfolder, adjust the RewriteRule to include the folder name:
RewriteRule ^(.*)$ /my_ci_project/index.php/$1 [L]
- Place the file in your project's root (CI3) or the
5. Check default routing
Make sure your project has a valid default controller set:
- CI3: Open
application/config/routes.phpand confirm:
Ensure$route['default_controller'] = 'Welcome'; // matches application/controllers/Welcome.phpWelcome.phpexists and has anindex()method. - CI4: Open
app/Config/Routes.phpand check:
Verify$routes->setDefaultController('Home'); // matches app/Controllers/Home.phpHome.phpexists and extendsBaseController.
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

