AWS EC2 Lamp Server "No 'Access-Control-Allow-Origin' is present on the requested resource. Therefore, the source is not allowed access."

I am currently hosting a PHP API page on an AWS EC2 Linux Lamp server ( as per this AWS documentation ). I previously hosted the same content on an Azure website, but decided to migrate hosting - from this point on I ran into some CORS issues when trying to access this page from a different domain (s):

XMLHttpRequest cannot load https://api.example.com/. No 'Access-Control-Allow-
Origin' header is present on the requested resource. Origin
'https://www.example.com' is therefore not allowed access.

      

I tried to configure my httpd.conf and .htaccess files to add the "Access-Control-Allow-Origin" header as in the previous hosting environment, but I can't get the headers to add my requests correctly. Here are my current files:

httpd.conf: http://pastie.org/private/fxqyepmrfnyf4ljzt3vcfa

.htaccess:

# Always set these headers.
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, content-type, origin, authorization, accept, client-security-token, X-Olaround-Debug-Mode, access-control-allow-headers, access-control-allow-methods, access-control-allow-origin, X-Frame-Options"

# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

      

However, when I check the headers on my request, I don't see the Access-Control-Allow-Origin header (but it's listed in the Access-Control-Request headers):

Host: api.example.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: POST
Origin: https://www.example.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36
Access-Control-Request-Headers: access-control-allow-headers, access-control-allow-methods, access-control-allow-origin, authorization, content-type
Accept: */*
Referer: https://www.example.com/index.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

      

I am assuming my .htaccess and httpd.conf files are not configured correctly, but how can I check this? I currently don't know which part (s) are failing or where to start debugging the problem. The implementation between my previous hosting is different due to the OS of the servers, so I'm trying to figure out where I'm getting lost.

Any advice where to look or what / how to debug to determine where the problem is coming from?

Thank.

EDIT Well, this is awkward - I thought I was uploading these files to my server, but later realized that my FTP client had chosen a permission not allowed to write. I forgot to change the ownership of directories and completely missed an error to write, so my changes never went through. He is working now.

+3


source to share





All Articles