Saturday, July 26, 2014

Configuring Apache to return CORS headers for Drupal Services

Here's what I did to configure Apache to return the proper CORS headers for my webapp consumption which is written in AngularJS:

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header always set Access-Control-Allow-Headers "accept, content-type"
Header always set Access-Control-Allow-Credentials "true"

That can be set in the VirtualHost tags for your server instance. Please note that Access-Control-Allow-Origin value shouldn't be set to * in a production environment. This should only be done for testing environments. The mod_headers module must be enabled in Apache for this configuration to work.

Having these options should be sufficient. But since I'm using Drupal 7 Services, it doesn't play well with pre-flight call which uses HTTP OPTIONS method. Drupal services will return a 404 even if the correct endpoint is specified when OPTIONS method is used.

Here's the additional config using mod_rewrite to return HTTP 200 for all OPTIONS requests:

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

7 comments:

afiah b said...

Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.

Java training in Chennai | Java training in Omr

Oracle training in Chennai

Java training in Chennai | Java training in Annanagar

Java training in Chennai | Java training institute in Chennai | Java course in Chennai

Unknown said...


Really very nice blog information for this one and more technical skills are improve,i like that kind of post.

rpa training in Chennai | rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training

genga g said...

Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
angularjs Training in bangalore

angularjs Training in bangalore

angularjs Training in btm

angularjs Training in electronic-city

angularjs online Training

angularjs Training in marathahalli

sakthi said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
rpa online training

Kavi said...

unique & very productive stuff..thanks for sharing.
Devops Training in Chennai | Devops Training Institute in Chennai

Unknown said...


Thanks for sharing this Post, Keep Updating such topics.
Deer Hunting Tips Camping Trips Guide DEER HUNTING TIPS travel touring tips

Unknown said...


I enjoyed over read your blog post. Your blog has nice information, I got good ideas from this amazing blog. I am always searching like this type of blog post. I hope I will see again.
Still Hunting Method
Hunting psych tips Survival Tips Travel Touring Tips