Answers.Body: <pre><code>&lt;FilesMatch &quot;\.(json)$&quot;&gt;
      &lt;IfModule mod_headers.c&gt;
          Header add Content-Type &quot;application/json&quot;
          Header add Access-Control-Allow-Methods &quot;GET,HEAD,POST,PUT,DELETE,OPTIONS&quot;
          Header add Access-Control-Allow-Headers &quot;Auth-Token,Content-Type&quot;
          Header add Access-Control-Allow-Origin &quot;*&quot;
      &lt;/IfModule&gt;
  &lt;/FilesMatch&gt;
  </code></pre>
  <p>This seems to work now, although it did not for quite some time.
  I don't know why but it works now...</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-30T10:43:01.630Z
Answers.LastActivityDate: 2023-03-30T10:43:01.630Z
Answers.UserDisplayName: Merc
Body: <p>I have a .json file that I want to place somewhere in my web folder and serve this if my frontend (another domain) requests it. I can open the file directly but not
  The problem is, that now I run into CORS problems.
  Normally if I return something in an action I can set the Headers and the Allow-Origin settings within my php file, but here I just want to get the .json file, nothing else.</p>
  <p>I think I need to adjust the .htaccess file within my <code>web</code> directory but so far I was not successful.</p>
  <p>I tried adding:</p>
  <pre><code>&lt;FilesMatch &quot;\.(json)$&quot;&gt;
      &lt;IfModule mod_headers.c&gt;
          Header set Access-Control-Allow-Origin &quot;*&quot;
      &lt;/IfModule&gt;
  &lt;/FilesMatch&gt;
  
  </code></pre>
  <p>within <code>/web/.htaccess</code></p>
  <p>Thank you for any hints how I can solve this problem.
  Cheers</p>
  
Tags: <htaccess><cors>
Title: How can I serve a .json file from the web folder without CORS problems?
ThreadText: # How can I serve a .json file from the web folder without CORS problems?
  
  (Asked by: username_0 on 2023-03-09)
  
  I have a .json file that I want to place somewhere in my web folder and serve this if my frontend (another domain) requests it. I can open the file directly but not
  The problem is, that now I run into CORS problems.
  Normally if I return something in an action I can set the Headers and the Allow-Origin settings within my php file, but here I just want to get the .json file, nothing else.
  
  I think I need to adjust the .htaccess file within my `web` directory but so far I was not successful.
  
  I tried adding:
  
  ```
  <FilesMatch "\.(json)$">
      <IfModule mod_headers.c>
          Header set Access-Control-Allow-Origin "*"
      </IfModule>
  </FilesMatch>
  
  ```
  
  within `/web/.htaccess`
  
  Thank you for any hints how I can solve this problem.
  Cheers
  
  **Tags:** htaccess, cors
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2023-03-30. Score: 1
  
  ```
  <FilesMatch "\.(json)$">
      <IfModule mod_headers.c>
          Header add Content-Type "application/json"
          Header add Access-Control-Allow-Methods "GET,HEAD,POST,PUT,DELETE,OPTIONS"
          Header add Access-Control-Allow-Headers "Auth-Token,Content-Type"
          Header add Access-Control-Allow-Origin "*"
      </IfModule>
  </FilesMatch>
  
  ```
  
  This seems to work now, although it did not for quite some time.
  I don't know why but it works now...
  
  
  ---
  
  