Cordova apps hit CORS problems in weird ways because they live in an awkward middle ground: web app rules, native wrapper, and sometimes a file:// or custom scheme origin depending on platform and plugin setup.

If your API sits behind Apache, you usually fix this in one place: response headers. The hard part is getting the exact combination right for simple requests, preflighted requests, credentials, and legacy Cordova behavior.

Here’s the reference I wish more teams had on hand.

What Cordova changes about CORS

A normal browser app usually comes from https://app.example.com. A Cordova app may come from:

  • file://
  • http://localhost
  • https://localhost
  • a custom origin injected by the WebView or platform

That matters because the browser enforces CORS based on the request Origin header. Your Apache config has to allow the actual origin your Cordova WebView sends, not the origin you assume it sends.

Before changing config, inspect the real request and response headers. I usually verify them with dev tools, curl, and a header checker like HeaderTest when I want a quick sanity check.

The minimum Apache setup

For a public API with no cookies or auth tied to browser credentials, * is often enough.

Enable mod_headers first:

sudo a2enmod headers
sudo systemctl reload apache2

Basic Apache config:

<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "*"
</IfModule>

That handles simple cross-origin GET requests.

A real-world example: GitHub’s API sends:

access-control-allow-origin: *
access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset, Warning

That’s a good reminder that CORS is not just about Allow-Origin. If your Cordova app needs to read custom response headers, you must expose them.

Copy-paste config for JSON APIs on Apache

This is the setup I use most often for APIs serving Cordova apps.

<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
    Header always set Access-Control-Expose-Headers "ETag, Link, Location, Retry-After"
</IfModule>

That covers:

  • common methods
  • JSON requests with Content-Type: application/json
  • bearer auth in Authorization
  • readable response headers in JavaScript

Handling preflight requests properly

Cordova apps often trigger preflight because they send:

  • Authorization
  • Content-Type: application/json
  • non-simple methods like PUT, PATCH, DELETE

If Apache doesn’t answer OPTIONS correctly, the real request never happens.

Use this:

<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
    Header always set Access-Control-Max-Age "86400"
</IfModule>

RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=204,L]

If you don’t want to use rewrite rules, another common pattern is handling OPTIONS in your app layer. That’s often cleaner if your backend already knows which routes are valid.

What matters is the response includes the CORS headers and returns a non-error status.

A quick test:

curl -i -X OPTIONS https://api.example.com/users \
  -H "Origin: http://localhost" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: content-type, authorization"

You want to see something like:

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: http://localhost
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With
Access-Control-Max-Age: 86400

When * is wrong: credentials

If your Cordova app sends cookies or HTTP auth with:

fetch("https://api.example.com/me", {
  credentials: "include"
});

then this Apache config is broken:

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Credentials "true"

Browsers reject Allow-Credentials: true with wildcard origin. You must echo a specific origin.

Apache config for credentialed Cordova requests

This is the safer pattern:

SetEnvIf Origin "^https?://(localhost|app\.example\.com)$" ACAO=$0

<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "%{ACAO}e" env=ACAO
    Header always set Access-Control-Allow-Credentials "true" env=ACAO
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE, OPTIONS" env=ACAO
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With" env=ACAO
    Header always set Vary "Origin" env=ACAO
</IfModule>

A few opinions from painful experience:

  • Always whitelist origins with a regex or explicit list.
  • Always send Vary: Origin when origin changes dynamically.
  • Never reflect arbitrary origins unless you genuinely want any site on earth to use your API.

For Cordova, http://localhost is a common origin on modern setups, but check your actual app. Some plugins and WebView versions differ.

Exposing headers Cordova code needs

Your JavaScript can only read a limited set of response headers unless you expose them.

Say your API returns pagination and rate limit data:

Header always set Access-Control-Expose-Headers "ETag, Link, Location, Retry-After, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset"

Then your Cordova code can do:

const res = await fetch("https://api.example.com/items");
console.log(res.headers.get("ETag"));
console.log(res.headers.get("Link"));
console.log(res.headers.get("X-RateLimit-Remaining"));

Without Access-Control-Expose-Headers, those calls usually return null even though the headers are plainly visible in network tools.

GitHub’s exposed header list is a good model for APIs that expect browser clients to inspect metadata:

access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset, Warning

Per-directory .htaccess example

If you can’t edit the main Apache vhost config, use .htaccess in the API directory:

<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
    Header always set Access-Control-Expose-Headers "ETag, Link, Location, Retry-After"
</IfModule>

RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^.*$ - [R=204,L]

This works only if Apache allows overrides:

AllowOverride All

I still prefer vhost config when possible. .htaccess is convenient, but it’s slower and easier to lose track of.

Common Cordova CORS failures

1. Missing Authorization in allowed headers

Symptom: preflight fails before your API sees the real request.

Fix:

Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"

2. Using wildcard origin with cookies

Symptom: browser says credentials mode is incompatible with *.

Fix: return the exact origin and add:

Header always set Access-Control-Allow-Credentials "true"
Header always set Vary "Origin"

3. Forgetting OPTIONS

Symptom: GET works, POST with JSON fails.

Fix: handle preflight and include OPTIONS in allowed methods.

4. Reading headers that aren’t exposed

Symptom: fetch() succeeds, but res.headers.get("ETag") is null.

Fix:

Header always set Access-Control-Expose-Headers "ETag, Link, Location"

5. Confusing CORS with CSP

CORS controls which origins can read responses. CSP controls what the app is allowed to load and execute. Cordova apps often need both tuned correctly. If you’re tightening script, connect, or frame policies too, csp-guide.com is worth keeping nearby.

Public API, token auth, no cookies

<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
    Header always set Access-Control-Expose-Headers "ETag, Link, Location, Retry-After"
    Header always set Access-Control-Max-Age "86400"
</IfModule>
SetEnvIf Origin "^http://localhost$" ACAO=$0

<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "%{ACAO}e" env=ACAO
    Header always set Access-Control-Allow-Credentials "true" env=ACAO
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE, OPTIONS" env=ACAO
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With" env=ACAO
    Header always set Access-Control-Expose-Headers "ETag, Link, Location, Retry-After" env=ACAO
    Header always set Access-Control-Max-Age "86400" env=ACAO
    Header always set Vary "Origin" env=ACAO
</IfModule>

Fast debugging checklist

When a Cordova request fails, I check these in order:

  1. What exact Origin did the app send?
  2. Did Apache return Access-Control-Allow-Origin?
  3. If preflight happened, did OPTIONS succeed?
  4. Does Access-Control-Allow-Headers include every requested header?
  5. Are credentials involved?
  6. Am I trying to read unexposed response headers?

If you answer those six, most CORS issues stop being mysterious and start being mechanical.

That’s really what you want with Cordova and Apache: boring, predictable headers.