This is the .htaccess
code I'm using to allow access to two domains to serve font files. The way it works is by:
- Setting an environment variable called
AccessControlAllowOrigin
to the matching group from theOrigin
string. If there is no matching group, then the variable will be empty. - The
FilesMatch
allows only font files, then sets the CORS headers only if theAccessControlAllowOrigin
environment is not empty. - Also, only allow
GET
requests specifically (not sure this is necessary, but other code like this allows PUT, DELETE, etc)
# allows font loads from dsriseah.com or localhost:8080 for dev
<IfModule mod_headers.c>
SetEnvIf Origin "(http(s)?://dsriseah.com)|(http(s)?://localhost:8080)$" AccessControlAllowOrigin=$0
<FilesMatch ".(eot|otf|svg|ttf|woff|woff2?)$">
Header set Access-Control-Allow-Origin "*" env=AccessControlAllowOrigin
Header set Access-Control-Allow-Methods "GET" env=AccessControlAllowOrigin
</FilesMatch>
</IfModule>