How do I enable CORS in an online server using AJAX and PHP to get font for use on another BigCartel site

I am currently using BigCartel to design a website.

I wish to use a custom font. No matter which server I use (currently a free Hostinger server) I cannot enable CORS using htaccess.

Added to .htaccess:

#   Header always add Access-Control-Allow-Origin "*"
Header always add Access-Control-Allow-Methods "POST, GET, PUT, OPTIONS, PATCH, DELETE"
Header always add Access-Control-Allow-Headers "X-Accept-Charset,X-Accept,Content-Type"
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L,E=HTTP_ORIGIN:%{HTTP:ORIGIN}]]

CSS:

@font-face {
font-family: FontName;
src: url(http://WEBSITE_URL.com/fontname.woff);
}

Answers


Most of the free hosting will not provide support for all the files. But having PHP support is one of the great things. So what you can do is, use PHP to serve the files in the correct format.

So for your issue, if you have font-name.woff, which has application/x-font-woff as the MIME Type, what you can do is, create a proxy PHP script that does something like this:

<?php
  ob_start();
  header("Content-type: application/x-font-woff");
  echo file_get_contents("file-name.woff");
?>

Make it generic and serve all the files this way:

<?php
  ob_start();
  header("Content-type: application/x-font-woff");
  echo file_get_contents("{$_GET["file"]}.woff");
?>

And call it as: font.php?file=file-name. Hope this helps.

Additionally, to incorporate CORS into PHP file:

<?php
  header("Access-Control-Allow-Origin: *");
  ob_start();
  header("Content-type: application/x-font-woff");
  echo file_get_contents("{$_GET["file"]}.woff");
?>

Need Your Help

Permanently disable shift in Qt when the GUI is running?

c++ qt qtableview qkeyevent

I'm using a QTableView where I show a list of Icons, the user can select some icons with mouse and control key button, and I'm able to handle these selections. But I want to disable the use of shif...

How to mock an interface that extends IEnumerable

c# mocking ienumerable moq

I'm using Moq and I have the following interface: