Laravel 5.6.12 单POST页面出现ERR_TOO_MANY_REDIRECTS问题求助
Hey there, let’s dig into this ERR_TOO_MANY_REDIRECTS issue you’re facing with your Laravel 5.6.12 app. It’s frustrating when something works locally but breaks after deployment, especially after 24 hours of troubleshooting—let’s break down possible causes and actionable fixes:
1. HTTPS/HTTP Redirect Mismatch
Most production environments use HTTPS, while local setups often stick to HTTP. This mismatch can trigger endless redirects if your app or server is forcing HTTPS but your form/routing is still pointing to HTTP.
- Double-check your
.envfile’sAPP_URLsetting—make sure it’s set to your production HTTPS address (e.g.,APP_URL=https://yourdomain.com). - Ensure your form uses Laravel’s helper functions like
route()orurl()to generate the POST action instead of hardcoding HTTP URLs. This ensures the correct protocol is used. - Verify your server config (Nginx/Apache) for HTTPS enforcement rules—make sure they aren’t conflicting with Laravel’s internal redirect logic.
2. CSRF Token Issues
Laravel requires valid CSRF tokens for POST requests. If the token is missing, invalid, or can’t be stored (due to session issues), it often redirects users—creating a loop if that redirect leads back to the form.
- Confirm your form includes the
@csrfdirective (Laravel 5.6 supports this; older versions use{{ csrf_field() }}). - Check your
.envfile’sSESSION_DOMAINsetting—local might leave this blank, but production needs your actual domain (e.g.,SESSION_DOMAIN=yourdomain.com). - Ensure the
storage/framework/sessionsdirectory has proper read/write permissions on your server. Laravel needs access to store sessions, and missing permissions can break CSRF validation.
3. Missing or Misconfigured POST Route
You mentioned the GET route Route::get('/requests/new', 'RequestsController@create');, but do you have a corresponding POST route for submitting the form? Without it, Laravel might redirect unexpectedly.
- Define the POST route explicitly if you haven’t already:
Route::post('/requests', 'RequestsController@store'); - Run
php artisan route:liston your server to confirm the POST route exists and points to the correct controller method. - Check your form’s
actionattribute to make sure it matches the POST route’s path or name (e.g.,action="{{ route('requests.store') }}").
4. Middleware Redirect Loops
Middleware like auth can redirect unauthenticated users to the login page—but if that login flow has conflicting logic, or if the user’s authentication state isn’t being recognized properly, it can loop.
- Look at your
RequestsControllerconstructor—does it use theauthmiddleware? If so, confirm the user is actually logged in on production (local might auto-login while production requires proper authentication). - Check any custom middleware you’ve added for faulty redirect logic (e.g., redirecting to the same route over and over).
5. Database/Logic-Driven Redirects
You mentioned the form fails to insert into the database. If your store method redirects back to the form on failure, and the failure reason persists (like mismatched database constraints), this creates a loop.
- Compare your production database schema to your local one—ensure fields have the same data types, lengths, and required statuses (e.g., a required field missing on production but optional locally).
- Add logging to your
storemethod to pinpoint the failure:use Illuminate\Support\Facades\Log; public function store(Request $request) { try { // Your insertion logic here } catch (\Exception $e) { Log::error('Request insertion failed: ' . $e->getMessage()); // Temporarily replace redirect with a plain error to test return response('Insertion failed: ' . $e->getMessage(), 500); } } - Temporarily comment out any redirect-on-failure logic to see if the loop stops—this will help confirm if the database issue is triggering the redirect cycle.
- Temporarily disable CSRF validation (only for testing, never in production): Add your POST route to the
$exceptarray inapp/Http/Middleware/VerifyCsrfToken.php. If the redirect stops, you know CSRF is the culprit. - Use Postman to send a direct POST request to your route—this skips browser redirects and lets you see the raw error response, which might reveal the root cause.
Hopefully one of these steps helps you track down the issue!
内容的提问来源于stack exchange,提问作者Grem357

