CodeIgniter服务器测试返回404?RESTful API部署问题求助
Hey there, let’s work through this 404 issue on your production server—this is super common when moving CodeIgniter apps between environments, especially with the restserver library. Here’s a step-by-step breakdown of the most likely fixes to check:
1. Verify URL Rewrite & Server Configuration
First up, let’s rule out server-level issues. CodeIgniter relies on URL rewriting to remove index.php from requests, and production servers (often Linux-based) are stricter than dev environments (maybe Windows).
For Apache:
- Make sure the
mod_rewritemodule is enabled. You can check this viaphpinfo()or by runninga2enmod rewriteon the server. - Double-check your
.htaccessfile in theci-serverroot directory. It should look something like this:RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] - Ensure the server’s
AllowOverridedirective is set toAllfor your project directory (in Apache’s site config), otherwise the.htaccessrules won’t apply.
- Make sure the
For Nginx:
- Update your server block to include a location rule that routes requests to CodeIgniter’s
index.php:location /ci-server { try_files $uri $uri/ /ci-server/index.php?$query_string; } - Don’t forget to reload Nginx after making changes (
sudo systemctl reload nginx).
- Update your server block to include a location rule that routes requests to CodeIgniter’s
2. Double-Check the base_url Configuration
Even though you said you updated this, small typos can break everything:
- Make sure your production
base_urlends with a trailing slash, likehttp://99.40.143.100/ci-server/—missing this can mess up route resolution. - To confirm it’s applied correctly, create a quick test controller:
Visit<?php class Test extends CI_Controller { public function index() { echo base_url(); } }http://99.40.143.100/ci-server/testto see if it outputs the correct URL.
3. Validate REST Controller & Route Setup
CodeIgniter and the restserver library have strict naming rules that might be ignored in your dev environment:
- Controller Case Sensitivity: Linux servers care about file and class names. If your controller is named
Users.php, the class must beclass Users extends REST_Controller(capital U). A lowercaseusers.phpwon’t work here. - REST Routes: Check
application/config/routes.phpto ensure your API routes are correctly mapped. For example:$route['api/users'] = 'users/index'; - Restserver Config: Open
application/config/rest.phpand confirm settings like$config['rest_default_format']are set correctly (e.g.,json), and there are no IP restrictions or auth rules accidentally blocking requests.
4. Check File Permissions
Production servers often have stricter permission settings:
- Ensure the
application/cache,application/logs, andapplicationdirectories are writable by the web server user (usuallywww-dataorapache). You can set permissions with:
Permissions issues can prevent CodeIgniter from loading necessary files, leading to 404s.chown -R www-data:www-data /path/to/ci-server/application chmod -R 755 /path/to/ci-server/application
5. Test Basic Requests to Isolate the Issue
Let’s narrow down where the problem is:
- First, visit
http://99.40.143.100/ci-server/index.php—if this returns a 404, the issue is with your server’s document root or project path, not the API itself. - If the default CodeIgniter page loads, try accessing your API with
index.phpincluded, likehttp://99.40.143.100/ci-server/index.php/api/users. If this works, the problem is definitely URL rewriting.
Start with these checks—9 times out of 10, it’s one of these issues. Let me know if you hit a snag!
内容的提问来源于stack exchange,提问作者anta40

