Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify entire subdomain #30

Closed
lloydmeta opened this issue Jul 13, 2013 · 9 comments
Closed

Specify entire subdomain #30

lloydmeta opened this issue Jul 13, 2013 · 9 comments

Comments

@lloydmeta
Copy link

Is there a way to specify that an entire subdomain (let's say api.example.com) should support CORS?

In particular, I'm looking to get this working with a Grape app mounted inside of Padrino...

@cyu
Copy link
Owner

cyu commented Jul 14, 2013

Try this:

config.middleware.use Rack::Cors do
  allow do
    origins '*.example.com'
    resource '*', :headers => :any, :methods => [:get, :post, :options]
  end
end

UPDATE: This example doesn't work. Instead use the regular expression example below.

@ramanbuttar
Copy link

origins '*.example.com' doesn't work. Wildcard is not being read as intended.

@buren
Copy link

buren commented Jul 20, 2016

@ramanbuttar is absolutely right. origins '*.example.com' will not allow test.example.com

@ryanwjackson
Copy link

did this ever get resolved? Is there a way around it?

@cyu
Copy link
Owner

cyu commented Aug 7, 2016

Sorry - I don't know why I ever said you can do that. Wildcard doesn't work, use a regular expression instead:

config.middleware.use Rack::Cors do
  allow do
    origins /http:\/\/(.*?)\.example\.com/
    resource '*', :headers => :any, :methods => [:get, :post, :options]
  end
end

@imgarylai
Copy link

imgarylai commented Feb 9, 2018

This is my regex example:

/\Ahttps?:\/\/([a-zA-Z\d-]+\.){0,}example\.com\/?\z/

My example can match https and end up with top level domain. For example https://example.com.hello.world is not a valid domain.

@raywu
Copy link

raywu commented Jun 5, 2018

Does origing have to match http:// or https://? Matching subdomain.example.com has worked for me, but the doc is unclear.

/^(.*\.|)example\.com$/ might work (regexr)

@sakirtemel
Copy link

sakirtemel commented Nov 15, 2018

I've created a wiki for that as it might be useful for the others, later maybe it can be moved to README. Thanks @raywu for the regex and the link it was useful, I copied actually that one to the wiki

@imi56
Copy link

imi56 commented Sep 4, 2021

Sorry - I don't know why I ever said you can do that. Wildcard doesn't work, use a regular expression instead:

config.middleware.use Rack::Cors do
  allow do
    origins /http:\/\/(.*?)\.example\.com/
    resource '*', :headers => :any, :methods => [:get, :post, :options]
  end
end

This will match anything.example.com.co.in as well which might not be desired expectation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants