0

I am trying to add an Access-Control-Origin header in .htaccess for all URIs ending with .json. I cannot use <FilesMatch> as my paths are rewritten by mod_rewrite. (Or if I can it doesn't work.)

I found on Stack that it should be possible to do it with an env variable:

<IfModule mod_headers.c>    
    SetEnvIf Request_URI "\.json$" IS_JSON=TRUE
    # ".json$" yields the same result
    Header set Access-Control-Allow-Origin "*" env=IS_JSON
    # "Header add" yields the same result
</IfModule>

But it does never add the header.

I tried using a snipper from another stack answer to check if the env variable is there using:

RewriteCond %{REQUEST_URI} !echo.php
RewriteRule .* echo.php?uri=%{REQUEST_URI}&json=%{ENV:IS_JSON} [L]

And it really was true.

When I remove the condition and leave it as:

Header set Access-Control-Allow-Origin "*"

the header is added, so the mod is loaded, but I would like to limit it for ".json$" only.

What am I doing wrong?

Community
  • 1
  • 1
Jakub Kotrs
  • 5,823
  • 1
  • 14
  • 30

1 Answers1

0

Instead of Header you probably meant to use RequestHeader to send custom headers to your code

SetEnvIf Request_URI \.json$ IS_JSON
RequestHeader set Access-Control-Allow-Origin "*" env=IS_JSON
anubhava
  • 761,203
  • 64
  • 569
  • 643