AMP Access Control Allow Source Origin header Issue

前端 未结 3 529
北荒
北荒 2020-12-09 22:38

How can I resolve the error below;

Failed to load resource: the server responded with a status of 500 (Internal Server Error) cdn.ampproject.org/v0.

3条回答
  •  悲哀的现实
    2020-12-09 23:37

    This may save a head ache or two. I went round in circles quite a while;

    Be aware in the recognized answer above that if https://example.com happens to be a domain name with hyphens in it, the string replacement will not give the desired result.

    For example https://www.my-domain.com needs to be turned into https://www-my--domain-com and it won't be.

    header("Access-Control-Allow-Origin: ". str_replace('.', '-','https://www.my-domain.com') .".cdn.ampproject.org");
    

    Needs to be

    header("Access-Control-Allow-Origin: https://www-my--domain.com.cdn.ampproject.org");
    

    So

    $h = 'https://www.my-domain.com';
    $h = str_replace('-', '--',$h);
    $h = str_replace('.', '-',$h).'.cdn.ampproject.org';
    

    then

    header("Access-Control-Allow-Origin: " . $h);
    

提交回复
热议问题