CodeIgniter 3.1在MAMP环境下自定义路由无法正常工作
Hey there, let's work through fixing your CodeIgniter routing issues step by step — I’ve dealt with this exact frustration before when setting up MAMP for CI projects, so I know what to check first.
1. Make sure Apache’s mod_rewrite is enabled
CodeIgniter’s clean URLs rely on this module, and it’s often disabled by default in MAMP. Here’s how to turn it on:
- Open MAMP, go to Preferences > Apache
- Click "Open httpd.conf" to edit the Apache config file
- Find the line that says
#LoadModule rewrite_module modules/mod_rewrite.so— remove the leading#to uncomment it - Save the file and restart Apache from the MAMP control panel
2. Verify CodeIgniter’s core config settings
Head to application/config/config.php and double-check these critical values:
- base_url: Set this to your project’s full URL, e.g.,
$config['base_url'] = 'http://localhost/your-ci-project/';(don’t leave this blank or use an incorrect path) - index_page: Set this to an empty string to remove
index.phpfrom URLs:$config['index_page'] = ''; - uri_protocol: If routes are still wonky, try switching this to
REQUEST_URI:$config['uri_protocol'] = 'REQUEST_URI';(AUTO can sometimes fail in MAMP environments)
3. Fix your .htaccess file
First, ensure your .htaccess is placed in your CI project’s root folder (same level as the application and system directories). Then use this MAMP-compatible code:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
If your project is in a subfolder of MAMP’s htdocs (e.g., htdocs/my-ci-app), double-check that your base_url matches this path — the .htaccess above should work without extra adjustments as long as base_url is correct.
4. Check Apache’s AllowOverride setting
Even if mod_rewrite is enabled, Apache might block .htaccess rules by default. Edit your httpd.conf again and find the <Directory> block for your MAMP htdocs folder (usually /Applications/MAMP/htdocs). Make sure AllowOverride is set to All:
<Directory "/Applications/MAMP/htdocs"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>
Save and restart Apache after making this change.
5. Validate your route rules and project path
- If accessing
localhost/Welcomegives no response, confirm your project is directly in MAMP’shtdocsfolder (or adjust the URL to include your subfolder, e.g.,localhost/my-ci-app/Welcome) - In
application/config/routes.php, ensure your custom routes follow the correct format:$route['custom-path'] = 'ControllerName/methodName'; - Double-check the default controller is set:
$route['default_controller'] = 'Welcome';
6. Clear CodeIgniter’s cache
Sometimes cached configs can prevent changes from taking effect. Delete all files in application/cache/ (if any exist) and temporarily set $config['cache_enabled'] = FALSE; in config.php to rule out caching issues.
Go through these steps one by one, and you should get your routes working again. Let me know if you hit any snags along the way!
内容的提问来源于stack exchange,提问作者S. Edward

