1

Trying to implement the following:

RewriteRule \.ttf$ - [E=cors:1]
Header set Access-Control-Allow-Origin "*.example.com" env=cors:1
Header set Access-Control-Allow-Origin "*.sadface.com" env=!cors:1 

Then requesting:

$ curl -I http://www.example.com/font.ttf
...
Access-Control-Allow-Origin: *.sadface.com

UPDATE

Thanks to Jenny D I've figured out that the rewrite rules elsewhere were doing a local redirect to index.php so I've modified to the following:

# Set CORS domain for fonts.
RewriteCond %{QUERY_STRING} \.ttf
RewriteRule ^(.*)$ $1 [E=cors:1]
Header set Access-Control-Allow-Origin "*.example.com" env=cors:1
Header set Access-Control-Allow-Origin "*.sadface.com" env=!cors:1

This provides the following log entry:

RewriteCond: input='q=font.ttf' pattern='\\.ttf' => matched

Despite this however I'm still getting Access-Control-Allow-Origin: *.sadface.com

UPDATE 2:

Looks like env=cors:1 isn't doing what I expect, changing to the following fixed the issue:

Header set Access-Control-Allow-Origin "*.example.com" env=cors
  • What does your rewrite log say? – Jenny D says Reinstate Monica Apr 20 '15 at 10:09
  • Oh thanks, never knew there was one! Looks like my request is rewriting to index.php as per RewriteRule ^ index.php [L] however given this is a Drupal site I'd expect the font.ttf to be rewritten to ?q=font.ttf but for some reason my query string is be evaluated as empty. – DanH Apr 20 '15 at 11:00
  • Logs are amazingly useful things :-) I'm glad you found the issue, but I'm also voting to close this question as a duplicate of the main mod_rewrite question. This isn't because you're bad or your question is, but because the answer (check the logs) can be found there, and by pointing your question to it we're making it easier for other people to find that canonical post and maybe finding the problem faster. – Jenny D says Reinstate Monica Apr 20 '15 at 12:41
2

Due to being a Drupal site, all non-file/directory requests were redirecting to index.php?q=$1. I instead needed to evaluate the query string, as per the following:

# Set CORS domain for fonts.
RewriteCond %{QUERY_STRING} \.ttf
RewriteRule ^(.*)$ $1 [E=cors:1]
Header set Access-Control-Allow-Origin "*.example.com" env=cors

Not the answer you're looking for? Browse other questions tagged or ask your own question.