Skip to content

Access Controll Allow Origin (CORS) for multiple matching domains using .htaccess

A while ago, I stumbled upon working on a bare metal server with LAMP stack installed in it. I have to allow CORS in several domains that consumes some of its API. My only option was using .htaccess and it is the fastest route I could get. So what I did is add this lines in .htaccess:

firs “IF” block is to make sure we have mod_headers extension enabled. This extension allows to modify headers of reqeusts via htaccess.

Next we use “SetEnvIf” the Origin matches the following domains in our regEx block which is:

then we set env variable “origin_is” if it matches the regex criteria above. Next is we set the header always with CORS using the following command code

That’s it!

Published inapicodingservers

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *