Apache · Configuration and operations

How do you handle CORS in Apache?

Mediumcorsheadersbrowser securityUpdated Oct 2026
Quiz this track

Short answer

CORS is enforced by the browser, so Apache must return the right Access-Control-Allow-Origin and related headers, handle OPTIONS preflight and vary responses by Origin. Never reflect arbitrary origins when credentials are allowed.

CORS is not a server-side access control mechanism. The browser sends an Origin header, the server decides whether to allow it, and the browser blocks the response if the headers do not match. Non-browser clients ignore CORS entirely, so authentication and authorization still need to be correct on the server.

apache
SetEnvIf Origin "^(https://(app|admin)[.]example[.]com)$" CORS_ORIGIN=$1
Header always set Access-Control-Allow-Origin "%{CORS_ORIGIN}e" env=CORS_ORIGIN
Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header always set Access-Control-Allow-Headers "Authorization, Content-Type"
Header always set Access-Control-Allow-Credentials "true"
Header always set Vary "Origin"

RewriteEngine On
RewriteCond %{REQUEST_METHOD} =OPTIONS
RewriteRule ^ - [R=204,L]
Allow only known origins and answer preflight requests.

Rules that prevent mistakes

  • Access-Control-Allow-Origin must be one origin or *, never a list
  • Do not combine * with Access-Control-Allow-Credentials: true
  • Echo an origin only after matching it against an allowlist
  • Send Vary: Origin so caches do not mix responses across origins
  • Preflight requests use OPTIONS and need a successful, header-bearing response
  • CORS does not protect APIs from curl, scripts or server-to-server calls

For a simple public API, a literal wildcard is fine. For cookie or bearer-token flows, the allowlist must be explicit and the server must reject unknown origins rather than reflecting them. If Apache proxies the API, decide whether Apache or the backend owns CORS headers to avoid duplicates.

A browser error is not proof that the API is secure. CORS only limits what browser JavaScript may read; it does not authenticate the caller.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why can't Access-Control-Allow-Origin contain multiple origins?
  2. 2What happens during a CORS preflight?
  3. 3Why is reflecting the Origin header dangerous?
  4. 4Does CORS protect an API from non-browser clients?

Say it in the interview

CORS is a browser policy. I return an allowlisted origin, the allowed methods and headers, and handle OPTIONS preflight, and when credentials are enabled I never use a wildcard and echo an origin only after matching it. I add Vary: Origin for caches. I also make clear that CORS is not authentication, because non-browser clients ignore it.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Apache mock interview and get scored feedback on depth, clarity and confidence.