Ubuntu下XAMPP运行CodeIgniter出现404错误求助
Hey JuanF, let's figure out why your existing CodeIgniter app is throwing 404 errors on Ubuntu + XAMPP while new projects work fine. Since fresh setups run without issues, the problem is tied to your app's specific configuration or file setup. Here are step-by-step checks and fixes:
1. Verify CodeIgniter Core Configuration
First, check your app's core config file at application/config/config.php:
- Ensure
$config['base_url']is set correctly to your project's root URL, e.g.:
Leaving this empty or using an incorrect path can break routing entirely.$config['base_url'] = 'http://localhost/your-project-folder/'; - Set
$config['index_page']to an empty string to let .htaccess handle removingindex.php:$config['index_page'] = ''; - Adjust
$config['uri_protocol']if needed—Ubuntu's XAMPP sometimes requiresREQUEST_URIinstead of the default:$config['uri_protocol'] = 'REQUEST_URI';
2. Check .htaccess Placement, Permissions, and mod_rewrite
- Location: Make sure your .htaccess file is in your project's root directory (same folder as
index.php), not inside theapplicationfolder. - Permissions: On Ubuntu, file permissions might block Apache from reading the .htaccess. Run this command (replace with your actual project path):
sudo chmod 644 /opt/lampp/htdocs/your-project/.htaccess - Enable mod_rewrite: Even if new projects work, double-check that mod_rewrite is enabled in XAMPP's Apache config. Open
/opt/lampp/etc/httpd.conf, find and uncomment this line:
Remove the#LoadModule rewrite_module modules/mod_rewrite.so#at the start, then restart Apache via the XAMPP control panel.
3. Update Apache's AllowOverride Setting
Apache might be ignoring your .htaccess rules if AllowOverride is set to None. Open /opt/lampp/etc/httpd.conf, locate the directory block for your web root (e.g., <Directory "/opt/lampp/htdocs">), and change:
AllowOverride None
to:
AllowOverride All
Restart Apache after making this change to apply the setting.
4. Check Routing and File Case Sensitivity
Ubuntu is case-sensitive, which is a common pitfall for CodeIgniter apps developed on Windows (which is case-insensitive):
- Open
application/config/routes.phpand confirm the default controller is set correctly, with matching case to the controller file. For example, if your controller isHome.php, the route should be:$route['default_controller'] = 'Home'; - Verify that all controller and method names match their file names exactly (e.g.,
UserController.phpcan't be referenced asusercontrollerin URLs or routes). - Check for custom routes that might be conflicting with your intended URLs.
5. Test Direct Index.php Access
To isolate whether the issue is with .htaccess or CodeIgniter routing, try accessing your app directly with index.php in the URL:
http://localhost/your-project/index.php/your-controller/your-method
- If this works, the problem is with .htaccess not being applied—go back to checking Apache's
AllowOverrideand mod_rewrite settings. - If this still throws a 404, the issue is with your controller/method existence, naming, or routing rules.
内容的提问来源于stack exchange,提问作者JuanF

