You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter服务器测试返回404?RESTful API部署问题求助

Troubleshooting 404 Errors on CodeIgniter REST API Production Server

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_rewrite module is enabled. You can check this via phpinfo() or by running a2enmod rewrite on the server.
    • Double-check your .htaccess file in the ci-server root 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 AllowOverride directive is set to All for your project directory (in Apache’s site config), otherwise the .htaccess rules won’t apply.
  • 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).

2. Double-Check the base_url Configuration

Even though you said you updated this, small typos can break everything:

  • Make sure your production base_url ends with a trailing slash, like http://99.40.143.100/ci-server/—missing this can mess up route resolution.
  • To confirm it’s applied correctly, create a quick test controller:
    <?php
    class Test extends CI_Controller {
        public function index() {
            echo base_url();
        }
    }
    
    Visit http://99.40.143.100/ci-server/test to 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 be class Users extends REST_Controller (capital U). A lowercase users.php won’t work here.
  • REST Routes: Check application/config/routes.php to ensure your API routes are correctly mapped. For example:
    $route['api/users'] = 'users/index';
    
  • Restserver Config: Open application/config/rest.php and 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, and application directories are writable by the web server user (usually www-data or apache). You can set permissions with:
    chown -R www-data:www-data /path/to/ci-server/application
    chmod -R 755 /path/to/ci-server/application
    
    Permissions issues can prevent CodeIgniter from loading necessary files, leading to 404s.

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.php included, like http://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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:11:52