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

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:

Possible Causes & Troubleshooting Steps

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 .env file’s APP_URL setting—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() or url() 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 @csrf directive (Laravel 5.6 supports this; older versions use {{ csrf_field() }}).
  • Check your .env file’s SESSION_DOMAIN setting—local might leave this blank, but production needs your actual domain (e.g., SESSION_DOMAIN=yourdomain.com).
  • Ensure the storage/framework/sessions directory 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:list on your server to confirm the POST route exists and points to the correct controller method.
  • Check your form’s action attribute 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 RequestsController constructor—does it use the auth middleware? 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 store method 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.
Quick Test to Narrow It Down
  • Temporarily disable CSRF validation (only for testing, never in production): Add your POST route to the $except array in app/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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:24:06