Laravel 5.2 CORS, GET not working with preflight OPTIONS

Laravel 5.2 CORS, GET not working with preflight OPTIONS

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel 5.2 CORS: GET Requests Failing After Preflight OPTIONS

If you are deploying a Laravel 5.2 application behind Apache and encountering a "Cross-Origin Request Blocked" error specifically for GET requests after enabling CORS, the issue usually isn't your controller logic. It is almost certainly an interaction between your custom middleware, the .htaccess rewrite rules, and how browsers handle preflight requests.

The error message explicitly states that the Access-Control-Allow-Origin header is missing. While you might have configured your middleware to return a 200 OK with headers when $request->getMethod() == "OPTIONS", the browser still blocks the subsequent GET request. This happens because Apache intercepts the OPTIONS request before it reaches your PHP application, effectively stripping out the necessary headers or altering the request flow in a way that breaks the CORS handshake.

The Hidden Culprit: .htaccess Rewrite Rules

In Laravel 5.2, if you have custom directives in your .htaccess file, specifically those handling authorization headers, they can inadvertently interfere with the OPTIONS method. The provided .htaccess snippet looks standard for passing through Authorization headers:

RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

While this looks harmless, it forces Apache to process every request with an authorization header. More importantly, if your hosting environment or specific .htaccess configuration is aggressive about handling OPTIONS requests (often used by load balancers or proxy servers for health checks), Apache might be terminating the request before your custom CORS middleware in App\Http\Middleware\CORS ever gets a chance to run.

If your middleware handles OPTIONS and returns an empty response with headers, but you aren't seeing those headers in the network tab for the subsequent GET request, it implies the browser is treating the OPTIONS response as a failure or the headers are not being persisted correctly in this specific Apache context.

Debugging the Middleware Flow

Your custom middleware logic attempts to handle both the preflight check and the actual data retrieval:

public function handle($request, Closure $next)
{
    header('Access-Control-Allow-Origin: *');

    // ALLOW OPTIONS METHOD
    $headers = [
        'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
        'Access-Control-Allow-Headers' => 'Content-Type, X-Auth-Token, Origin, Authorization'
    ];
    
    if ($request->getMethod() == "OPTIONS") {
        // The client-side application can set only headers allowed in Access-Control-Allow-Headers
        return Response::make('OK', 200, $headers);
    }

    $response = $next($request);
    foreach ($headers as $key => $value)
        $response->header($key, $value);
    
    return $response;
}

The logic here is sound for a standalone server (like Nginx or development environments), but Apache's behavior can differ. When you see the CORS error after an OPTIONS request succeeds in your browser console, it usually means the Access-Control-Allow-Origin header was not sent in the final response of the GET request.

In Laravel 5.2, the standard approach to fixing this without relying on third-party packages like barryvdh/laravel-cors is to ensure that the headers are applied globally to every response, not just conditionally inside the middleware's foreach loop at the end. The current code only appends headers to $response if the request was not OPTIONS. If Apache caches or rewrites the response in a way that bypasses the second half of your middleware, those headers vanish for the GET request.

Recommended Solution: Global Header Application

The most robust fix is to ensure the CORS headers are set on every response object regardless of the HTTP method. This prevents scenarios where the next() call or Apache's rewrite engine might return a response that lacks these specific headers.

Update your middleware as follows:

public function handle($request, Closure $next)
{
    // Set CORS headers on all responses immediately
    $response = Response::make('');
    
    $response->header('Access-Control-Allow-Origin', '*');
    $response->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');
    $response->header('Access-Control-Allow-Headers', 'Content-Type, X-Auth-Token, Origin, Authorization');
    
    if ($request->getMethod() == "OPTIONS") {
        // Preflight request handled immediately with headers already set
        return $response->setStatusCode(200);
    }

    // For non-OPTIONS requests, pass through and append headers to the actual response
    $actualResponse = $next($request);
    
    // Ensure headers are present on the final output
    $actualResponse->header('Access-Control-Allow-Origin', '*');
    $actualResponse->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');
    $actualResponse->header('Access-Control-Allow-Headers', 'Content-Type, X-Auth-Token, Origin, Authorization');

    return $actualResponse;
}

By explicitly setting the headers on the $response object before and after the next() call, you guarantee that even if Apache modifies the request flow or caches the OPTIONS response, the subsequent GET request will still carry the required Access-Control-Allow-Origin header. This approach eliminates the dependency on specific package versions in Laravel 5.2 and resolves the conflict between your .htaccess rules and the middleware's header injection strategy.

If you continue to face issues after this adjustment, consider checking your Apache error logs (/var/log/apache2/error.log or similar) for any warnings regarding Access-Control-Allow-Origin being stripped by security modules like ModSecurity.

Our network