CodeIgniter后台面板Google登录重定向404问题求助
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 (includinghttp/https, trailing slashes, and full path). For example, if your controller isWelcomeand the callback method isauth_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
- With index.php:
- Even a tiny difference (like missing
wwwor 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 iscontroller/method, so your callback URL should point toWelcome/auth_user(match your controller name exactly, case matters on Linux servers). - .htaccess Configuration: If you've removed
index.phpfrom URLs (set$config['index_page'] = '';), make sure your .htaccess file is properly configured to rewrite requests. Here's a standard working .htaccess for CodeIgniter:
Without this, requests toRewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]/Welcome/auth_userwill 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
Googleclass'sgetAuthenticatemethod to accept the code:public function getAuthenticate($code) { return $this->client->authenticate($code); } - Then in your
auth_usercontroller method, pass the code explicitly:
The default$this->google->getAuthenticate($_GET['code']);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.phpand add your callback URI to the CSRF exclusion list:
This tells CodeIgniter to skip CSRF checks for the OAuth callback route.$config['csrf_exclude_uris'] = array('Welcome/auth_user');
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_usermethod is marked aspublic(your code already does this, but double-check you didn't accidentally change it toprivate/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
codeparameter, 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

