# Access-Control-AllowOrigin : multiple domains

**URL:** <https://discourse.slimframework.com/t/access-control-alloworigin-multiple-domains/1342>\
**Category:** Questions\
**Created:** [March 29, 2017, 5:46pm UTC](https://discourse.slimframework.com/t/access-control-alloworigin-multiple-domains/1342 "2017-03-29T17:46:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jb.tellez](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jb.tellez](https://discourse.slimframework.com/u/jb.tellez)\
**Post date:** [March 29, 2017, 5:46pm UTC](https://discourse.slimframework.com/t/access-control-alloworigin-multiple-domains/1342/1 "2017-03-29T17:46:25Z")

</div>

Hello folks,

Is it possible to have multiple domains allowed with CORS?

E,g, could middleware be set up like…

```
return $response
        ->withHeader('Access-Control-Allow-Origin', 'https://somedomain.com')
        ->withHeader('Access-Control-Allow-Origin', 'http://otherdomain.com')

```

In my case, my staging server is at different domain than live site and I’d like to support both.

Thanks!

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [March 29, 2017, 6:23pm UTC](https://discourse.slimframework.com/t/access-control-alloworigin-multiple-domains/1342/2 "2017-03-29T18:23:27Z")

</div>

I know almost nothing about CORS, but looking at the [PSR-7 docs](http://www.php-fig.org/psr/psr-7/) you might try this:

```php
return $response
    ->withHeader('Access-Control-Allow-Origin', 'https://somedomain.com')
    ->withAddedHeader('Access-Control-Allow-Origin', 'http://otherdomain.com')

```

---

<div class="post-metadata">

**Author:** ![jb.tellez](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jb.tellez](https://discourse.slimframework.com/u/jb.tellez)\
**Post date:** [March 29, 2017, 8:00pm UTC](https://discourse.slimframework.com/t/access-control-alloworigin-multiple-domains/1342/3 "2017-03-29T20:00:56Z")

</div>

Thanks for the tip tflight! That didn’t work but I’ll check out those docs some more.

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [March 30, 2017, 4:08pm UTC](https://discourse.slimframework.com/t/access-control-alloworigin-multiple-domains/1342/4 "2017-03-30T16:08:51Z")

</div>

According to the [documentation at Mozilla](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Access-Control-Allow-Origin), you can have only one `Access-Control-Allow-Origin` header:

> A returned resource may have one Access-Control-Allow-Origin header, with the following syntax:

> Access-Control-Allow-Origin: \<origin\> | \*

You can use `*` for any origin:

```php
return $response
        ->withHeader('Access-Control-Allow-Origin', '*');

```

---

<div class="post-metadata">

**Author:** ![eko3alpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/eko3alpha/32/350_2.png) [@eko3alpha](https://discourse.slimframework.com/u/eko3alpha)\
**Post date:** [March 31, 2017, 3:46pm UTC](https://discourse.slimframework.com/t/access-control-alloworigin-multiple-domains/1342/5 "2017-03-31T15:46:43Z")

</div>

[Unfortunately you cannot](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin) specify multiple domains. You either need to create an .htaccess/apache rule: [credit](https://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains) which may or may not work for you if you don’t have access to apache modules.

```
<FilesMatch "\.(ttf|otf|eot|woff|js|css|woff2)$">
    <IfModule mod_headers.c>
        SetEnvIf Origin "^http(s)?:\/\/(www\.|dev\.|local\.)?(domain\.com|domain2\.com)$" AccessControlAllowOrigin=$0
        Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
    </IfModule>
</FilesMatch>

```

or have a wildcard which doesn’t make sense to me. It seems to either be an all or very restrictive approach, which then most dev’s will opt for the very easy wildcard ‘\*’ approach.

I wrote a [Slim middleware](https://github.com/eko3alpha/slim-cors-middleware), (which can be installed via composer) since I needed the ability to allow my API to be accessed by multiple domains and restrict what methods I allow. I also prefer having this ability in php instead of an .htaccess file. This is a slightly modified version of what I’m using since I’m using Slim’s DI Container. I moved all the settings into the same file. In other words make sure to TEST TEST TEST it as I’m not using this exact implementation.

CorsMiddleware.php

```
<?php
namespace Middleware;

class CorsMiddleware
{
    /**
     * Associative array with domain => [allowed methods] list
     * @var array
     */
    protected $cors = [
        'https://somedomain.com' => ['GET', 'POST'],
        'http://somedomain.com' => ['GET', 'POST'],
        'https://dev.somedomain.com' => ['DELETE', 'PUT', 'POST']
    ];

    /**
     * Middleware invokable class
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
     * @param \Psr\Http\Message\ResponseInterface $response PSR7 response
     * @param callable $next Next middleware
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function run($request, $response, $next)
    {
        $response = $next($request, $response);
        $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : 'none';
        return $this->getResponse($response, $origin, $this->cors);
    }

    /**
     * Gets allow method string of comma separated http verbs
     * @param string $origin origin domain
     * @param array $cors access list with methods
     * @return string comma delimited string of methods
     */
    private function getAllowedMethodsString($cors, $origin)
    {
        $methods = $cors[$origin];
        if (is_array($methods)) {
            $methods = implode(', ', $methods);
        }
        return $methods;
    }

    /**
     * Gets the proper origin header value
     * @param array $cors cors config
     * @param string $origin http_origin
     * @return string origin value
     */
    private function getOriginHeader($cors, $origin)
    {
        if (isset($cors['*'])) {
            return '*';
        }
        return $origin;
    }

    /**
     * Gets appropriate response object
     * @param \Psr\Http\Message\ResponseInterface $response PSR7 Response
     * @param string $origin origin domain
     * @param array $cors access list with methods
     * @return \Psr\Http\Message\ResponseInterface $response PSR7 Response
     */
    private function getResponse($response, $origin, $cors)
    {
        if (isset($cors['*'])) {
            $origin = '*';
        }

        if (!isset($cors[$origin])) {
            return $response;
        }

        return $response
        ->withHeader('Access-Control-Allow-Origin', $this->getOriginHeader($cors, $origin))
        ->withHeader('Access-Control-Allow-Methods', $this->getAllowedMethodsString($cors, $origin));
    }
}

```

index.php

```
.
.
$app->add('\Middleware\CorsMiddleware:run');
.
.

```

You may wonder why Allow-Methods may not appear to be working when you test ( you will test… right?!) , this got me as well, and I found the answer:

_Simple cross-origin requests generated outside this specification (such as cross-origin form submissions using GET or POST or cross-origin GET requests resulting from script elements) typically include user credentials, so resources conforming to this specification must always be prepared to expect simple cross-origin requests with credentials._

_Because of this, resources for which simple requests have significance other than retrieval must protect themselves from Cross-Site Request Forgery (CSRF) by requiring the inclusion of an unguessable token in the explicitly provided content of the request._

[credit](http://stackoverflow.com/a/42856304)

Also mind your protocols! if you want http and https you will need to include both in the $cors list. Hope that helps.

---

<div class="post-metadata">

**Author:** ![jb.tellez](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jb.tellez](https://discourse.slimframework.com/u/jb.tellez)\
**Post date:** [April 4, 2017, 6:56am UTC](https://discourse.slimframework.com/t/access-control-alloworigin-multiple-domains/1342/6 "2017-04-04T06:56:22Z")

</div>

That’s fantastic. I’ll test that out once I get a chance to wrap my head around the code. Thanks!

---

<div class="post-metadata">

**Author:** ![eko3alpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/eko3alpha/32/350_2.png) [@eko3alpha](https://discourse.slimframework.com/u/eko3alpha)\
**Post date:** [April 4, 2017, 5:43pm UTC](https://discourse.slimframework.com/t/access-control-alloworigin-multiple-domains/1342/7 "2017-04-04T17:43:06Z")

</div>

Hope it works for you, I threw the middleware up on [github](https://github.com/eko3alpha/slim-cors-middleware) and made some modifications and added examples.
