如何部署CodeIgniter网站至服务器?解决部署后404错误
Hey there, let's walk through why you're getting that 404 error after uploading your CodeIgniter project to the server—this is a super common gotcha, and we can fix it quickly!
1. Base URL Mismatch (Most Likely Culprit)
Your local setup uses localhost/project_name as the base, but the server needs the actual domain path. This throws off CodeIgniter's routing entirely.
- Open
application/config/config.php - Find the
$config['base_url']line and update it to match your server's path:
Don't forget the trailing slash—it matters for asset paths and routing.$config['base_url'] = 'http://www.domain_name.com/project_name/';
2. .htaccess Problems or Rewrite Rules
Local servers (like XAMPP/WAMP) often have rewrite enabled by default, but live servers might not. Plus, hidden .htaccess files can get left out when zipping/uploading.
- First, confirm your project root has an
.htaccessfile (it's hidden, so make sure you included it in your ZIP). For subdirectory deployments, the rules should look like this:RewriteEngine On RewriteBase /project_name/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] - If you're using Apache, check that
mod_rewriteis enabled (ask your host if you're unsure). For Nginx, you'll need to add equivalent rewrite rules to your server block.
3. Case Sensitivity (Linux vs Windows)
Local Windows machines ignore file case, but Linux servers don't. This is a sneaky one!
- CodeIgniter requires controller filenames to have a capitalized first letter, and the class name must match exactly. For example:
- Filename:
Controller_name.php(first letter capitalized) - Class inside:
class Controller_name extends CI_Controller
- Filename:
- If your local filename was
controller_name.php(all lowercase), the server won't recognize it—rename it to match the capitalization rule.
4. Incorrect File/Directory Permissions
The server needs access to write to cache and logs folders, and read access to all core files.
- Set
application/cacheandapplication/logsto755(or777if 755 doesn't work—though 755 is more secure) - Set all PHP files to
644and directories to755 - Avoid setting root or system files to
777—that's a major security risk.
5. Missing or Misconfigured Files
Double-check that you uploaded every file from your local project—sometimes hidden files or system folders get skipped when zipping.
- Verify
system/,application/, andindex.phpare all present in the server'sproject_namedirectory - Open
index.phpand confirm$system_pathand$application_folderpoint to the correct relative paths (they should match your local setup if the folder structure is identical)
6. Route Configuration Errors
If your routes are pointing to the wrong controller (or using incorrect case), you'll get a 404 even if everything else is right.
- Open
application/config/routes.phpand check your default controller:$route['default_controller'] = 'controller_name'; - Make sure the controller name here matches the actual class name (including case)
Quick Troubleshooting Checklist
- Did you include hidden files like
.htaccessin your ZIP upload? - Is
base_urlupdated to your server's domain and project subdirectory? - Is URL rewriting enabled on your server?
- Are controller filenames and class names properly capitalized?
- Do cache/log directories have the right permissions?
内容的提问来源于stack exchange,提问作者Tanmay

