Question

Vurtual (.htaccess) Subdomains CORS Issue - No 'Access-Control-Allow-Origin' header

I use .htaccess mod rewrite rules to redirect any (existing) subdomain URL request to the main website where those URLs are resolved internally:

** RewriteCond %{HTTP_HOST} ^(.).example.com [NC] RewriteRule (.) sites/index.php [NC,L]**

    eg. any **virtual-subdomain.example.com/some-page**
    would be processed by **example.com/sites/index.php** 

    absolute path:** /home/example.com/public_html/sites/index.php**

I am using some icon web fonts, not at CDN but nested within a dedicated folder within my main site (eg. example.com/fonts/, however, they are not displayed due to CORS policies and the error message I get in the console is as follows:

Access to font at ‘https://example.com/fonts/fontawesome/fontawesome-webfont.woff2?v=4.3.0’ from origin ‘https://subdomain.example.com’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

I’ve already tried implementing the following direction first at the beginning of my .htaccess file, then at the server conf file as well (/etc/apache2/sites-available/example.com.conf) :

<IfModule mod_headers.c> <FilesMatch “.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$”> Header set Access-Control-Allow-Origin “*” </FilesMatch> </IfModule>

I also tried with the alternative code specific for the subdomain scenario:

<ifmodule mod_headers.c=“”> SetEnvIf Origin “^(..example.com)$" ORIGIN_SUB_DOMAIN=$1 Header set Access-Control-Allow-Origin “%{ORIGIN_SUB_DOMAIN}e” env=ORIGIN_SUB_DOMAIN Header set Access-Control-Allow-Methods: "” Header set Access-Control-Allow-Headers: “Origin, X-Requested-With, Content-Type, Accept, Authorization” </ifmodule>

However I could not resolve the CORS issue and the error message still remains and the icons are not displayed in the browser (Crom, Firefox…).

Any idea on how should I resolve this issue or what am I doing wrong?

Thanks!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Ups… I’ve managed to figure out the culprit… actually I had some javascript errors in my code that messed up other on-page functionalities. Once I resolved it the font issue and CORS was resolved as well :)

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.