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

CodeIgniter后台面板Google登录重定向404问题求助

Troubleshooting Google OAuth 404 Redirect in CodeIgniter

Hey there, let's figure out why your Google login redirect is throwing a 404 error. I've gone through your code and here are the key areas to check, ordered by how likely they are to fix your issue:

1. Double-Check the Redirect URI (Most Common Fix)

This is the #1 cause of OAuth redirect 404s. Google requires an exact match between the redirect URI in your CodeIgniter config and what's set in your Google Developer Console.

  • Verify $config['google']['redirect_uri'] in your Google config file matches exactly what's in your Google Console (including http/https, trailing slashes, and full path). For example, if your controller is Welcome and the callback method is auth_user, the URI should look like:
    • With index.php: http://your-domain.com/index.php/Welcome/auth_user
    • Without index.php: http://your-domain.com/Welcome/auth_user
  • Even a tiny difference (like missing www or a trailing slash) will trigger a 404.

2. Validate CodeIgniter Routing & .htaccess

If your redirect URI is correct, the next step is ensuring CodeIgniter can route the request to your auth_user method:

  • Check Routes: Open application/config/routes.php — if you haven't set a custom route, the default rule is controller/method, so your callback URL should point to Welcome/auth_user (match your controller name exactly, case matters on Linux servers).
  • .htaccess Configuration: If you've removed index.php from URLs (set $config['index_page'] = '';), make sure your .htaccess file is properly configured to rewrite requests. Here's a standard working .htaccess for CodeIgniter:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    Without this, requests to /Welcome/auth_user will hit a 404 because the server can't find the file directly.

3. Fix the Google Library's Authenticate Method

Looking at your Google OAuth library code, the getAuthenticate() method doesn't accept the authorization code parameter, which can cause issues with token retrieval:

  • Update your Google class's getAuthenticate method to accept the code:
    public function getAuthenticate($code) {
        return $this->client->authenticate($code);
    }
    
  • Then in your auth_user controller method, pass the code explicitly:
    $this->google->getAuthenticate($_GET['code']);
    
    The default authenticate() method tries to pull the code from $_GET, but passing it directly avoids any issues with CodeIgniter's URL parsing.

4. Check CSRF Protection

If CodeIgniter's CSRF protection is enabled, it might block the Google callback request (since it doesn't include a CSRF token):

  • Open application/config/config.php and add your callback URI to the CSRF exclusion list:
    $config['csrf_exclude_uris'] = array('Welcome/auth_user');
    
    This tells CodeIgniter to skip CSRF checks for the OAuth callback route.

5. Verify Controller Accessibility

Make sure your controller and method are accessible:

  • Confirm your controller file is named Welcome.php (首字母大写 — CodeIgniter is case-sensitive on Linux systems) and the class is defined as:
    class Welcome extends CI_Controller {
        // ... your methods here
    }
    
  • Ensure the auth_user method is marked as public (your code already does this, but double-check you didn't accidentally change it to private/protected).

6. Debug the Callback Request

To confirm if the request is even reaching your auth_user method, add a quick debug log at the top of the method:

public function auth_user() {
    // Log the incoming request data
    file_put_contents(APPPATH.'logs/oauth_callback.log', print_r($_GET, true) . "\n", FILE_APPEND);
    // ... rest of your code
}

After trying the Google login again, check application/logs/oauth_callback.log:

  • If the file has the code parameter, the request is reaching your method — the issue is in your code logic.
  • If the file is empty, the request isn't getting to your controller, so the problem is with routing or redirect URI.

Bonus: Check Session Configuration

Ensure your CodeIgniter session is properly configured (in application/config/config.php) — if sessions are broken, it might interfere with the OAuth flow, but this is less likely to cause a direct 404.


内容的提问来源于stack exchange,提问作者Akhil

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:00:05