CodeIgniter3 HMVC模式下404错误问题求助
Let’s break down the most common fixes for the 404 error you’re facing in your CodeIgniter 3 HMVC project running on PHP7. I’ve worked through similar issues before, so here’s a step-by-step checklist to get things working:
1. Fix the base_url Configuration
Your current config.php has an empty base_url, which is a frequent culprit for routing issues in CI3—especially with HMVC. CodeIgniter relies on this value to properly resolve URIs and load HMVC modules correctly.
Update it to your actual project URL:
$config['base_url'] = 'http://localhost/your-project-name/'; // For local development // OR $config['base_url'] = 'https://your-production-domain.com/'; // For live site
2. Adjust the uri_protocol Setting
The AUTO setting for uri_protocol can be inconsistent across different server environments (Apache, Nginx, etc.). It might fail to detect the correct URI format, leading to unrecognized routes.
Try switching to REQUEST_URI first:
$config['uri_protocol'] = 'REQUEST_URI';
If that doesn’t work, test PATH_INFO instead—this is often more reliable for HMVC setups.
3. Verify HMVC Controller Structure
Since you’re using HMVC, make sure your service controller follows the correct directory and naming conventions:
- The controller file must be at
application/modules/service/controllers/Service.php - The class name must be
Service(首字母大写, matching the filename) - The default method should be
index()(this runs when you hit the base URL)
Example of a valid Service.php controller:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Service extends MX_Controller { public function index() { echo "Hello from Service Controller!"; } }
If the filename or class name is lowercase (e.g., service.php or class service), CI3 won’t recognize it, triggering a 404.
4. Tweak the .htaccess Rewrite Rule
Your current .htaccess rule uses a slash after index.php, which might not work on all Apache configurations (especially newer versions). Try replacing the rewrite rule with this version (using a ? instead of /):
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?$1 [L,QSA] </IfModule>
Also, confirm that the mod_rewrite module is enabled on your server. You can check this via phpinfo() or by asking your hosting provider.
5. Enable Debug Logs to Diagnose Hidden Issues
If the above steps don’t resolve the 404, turn on CodeIgniter’s logging to catch any underlying errors that might be causing the issue:
$config['log_threshold'] = 2; // Logs errors and debug messages
Check the application/logs directory for recent log files—they might reveal problems like missing modules, syntax errors in your controller, or permission issues.
6. Test with 404 Override (Temporary)
As a quick test, set the 404_override route to your service controller to see if CI can locate it:
$route['404_override'] = 'service';
If your service controller loads now, it means the original routing logic is failing—double-check your default_controller setting and ensure the HMVC module is properly registered.
内容的提问来源于stack exchange,提问作者Sanhita Sinha

