Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

A CORS preflight request gets redirected and the browser blocks it

ErrorRedirect is not allowed for a preflight request

Short answer

Before certain cross-origin requests, the browser sends an OPTIONS preflight. If the server answers that preflight with a redirect, some browsers refuse to follow it and block the real request. Call the final URL directly, stop the server redirecting that route, or change the request so no preflight happens.

  • Applies to Chrome, Firefox, Laravel 12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the preflight by hand and look at the status line and the Location header. A 301, 302, 307 or 308 here confirms a redirect.

    curl -i -X OPTIONS https://api.example.com/api/orders \
      -H "Origin: https://app.example.com" \
      -H "Access-Control-Request-Method: POST"
  2. 2Match what you see, then jump to its fix

    1. 1The curl output shows a Location header pointing to a different address.The URL your front end calls is not the final URLFix
    2. 2A plain GET works from the browser, but a POST with JSON or an Authorization header fails.The request triggers a preflight that the redirect then breaksFix

What does the error look like?

  • Redirect is not allowed for a preflight request
  • Reason: CORS request external redirect not allowed
  • Request requires preflight, which is disallowed to follow cross-origin redirects

Cause 1 of 2

The URL your front end calls is not the final URL

1Check

The redirect target is the address the server wants. A frequent source is a Laravel route declared with Route::redirect, which answers 302 unless you pass another code.

grep -rn "Route::redirect\|Route::permanentRedirect" routes

2Fix

Change the front-end URL to the address in Location. If you own the server, remove the redirect on the API route instead of redirecting.

// before
axios.post('https://api.example.com/api/orders', data);
// after: the URL from the Location header
axios.post('https://api.example.com/api/v2/orders', data);

3Verify

The OPTIONS request now returns a success status with no Location header.

curl -i -X OPTIONS https://api.example.com/api/v2/orders -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: POST"

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 2

The request triggers a preflight that the redirect then breaks

1Check

Requests with custom headers or non-simple methods are preflighted. Simple requests are not, so they are not blocked by this rule.

curl -i -X OPTIONS https://api.example.com/api/orders -H "Origin: https://app.example.com" -H "Access-Control-Request-Headers: content-type"

2Fix

Where you cannot remove the redirect, change the server or the request so no preflight is needed. Otherwise make a simple request first, read the final address from Response.url, and send the real request there. This workaround does not work when the preflight comes from an Authorization header.

const probe = await fetch('https://api.example.com/api/orders');
const finalUrl = probe.url;
await fetch(finalUrl, { method: 'POST', body: JSON.stringify(data) });

3Verify

The browser console shows no CORS error and the network tab shows the real request.

# open DevTools, Network tab, and filter by the API host

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Store the API base URL in one place and keep it equal to the final address.
  • Test every API route with a curl OPTIONS request after changing routes or the server's redirect rules.
  • Do not mix a redirecting route with a CORS-enabled API path.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Browser blocks your Laravel API with a CORS error

Response to preflight request doesn't pass access control check

Laravel answers CORS preflights itself through its HandleCors middleware, driven by config/cors.php.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.