Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CORS example for Apache with multiple domains
# Sets CORS headers for request from example1.com and example2.com pages
# for both SSL and non-SSL
SetEnvIf Origin "^https?://[^/]*(example1|example2)\.com$" ORIGIN=$0
Header set Access-Control-Allow-Origin %{ORIGIN}e env=ORIGIN
Header set Access-Control-Allow-Credentials "true" env=ORIGIN
# Always set Vary: Origin when it's possible you may send CORS headers
Header merge Vary Origin
@manuelgarcia

This comment has been minimized.

Copy link

@manuelgarcia manuelgarcia commented Oct 15, 2016

Thanks!

@mdimitris

This comment has been minimized.

Copy link

@mdimitris mdimitris commented Dec 2, 2016

It doesnt work for me...Am I doing smth wrong? Here is what I have in httpd.conf file:

# Sets CORS headers for request from example1.com and example2.com pages
# for both SSL and non-SSL
SetEnvIf Origin "^https?://[^/]*(example1|subdomain.example2)\.com$" ORIGIN=$0
Header set Access-Control-Allow-Origin %{ORIGIN}e env=ORIGIN
Header set Access-Control-Allow-Credentials "true" env=ORIGIN
# Always set Vary: Origin when it's possible you may send CORS headers
Header merge Vary Origin

And I receive the following:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://subdomain.example.com/scripts/loginFromPresta.php. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

@teknopaul

This comment has been minimized.

Copy link

@teknopaul teknopaul commented Jan 27, 2017

I had to add "always" to the Header set

Header always set Access-Control-Allow-Origin %{ORIGIN}e env=ORIGIN

This then sets the header, It ought to replace the header but this doe not work for me so I get multiple headers which is not permitted.

String struggling with CORS in Apache, someone needs to write the definitive mod_cors.

@blazejkrzak

This comment has been minimized.

Copy link

@blazejkrzak blazejkrzak commented Apr 15, 2018

This was what i was looking for!
Since firefox quantum (or maybe earlier) origins set on "*" does not work for xhr -> withCredentials. Your solution is what i needed. Works with header alwats set as @teknopaul suggested.

@fourmi-integree

This comment has been minimized.

Copy link

@fourmi-integree fourmi-integree commented Apr 20, 2018

very good / tx

question !
what is the fucntion of the "e" => ...%{ORIGIN}e... , a flag ?

@ericcgu

This comment has been minimized.

Copy link

@ericcgu ericcgu commented May 24, 2019

  1. Load Modules
LoadModule headers_module modules/mod_headers.so
LoadModule rewrite_module modules/mod_rewrite.so
  1. Change Folder in Bold to your target directory serving the endpoint:
<Directory "**/usr/local/apache2**">
    AllowOverride None
    Require all granted

    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
    Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
    Header always set Access-Control-Expose-Headers "Content-Security-Policy, Location"
    Header always set Access-Control-Max-Age "600"

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

</Directory>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment