How to Configure CORS in the Rewrite Rules (.htaccess)

Machine-translated (anthropic, claude-haiku-5-5) from the Chinese original. Read the original in 中文

Contents

Allow cross-origin requests from all domains

text
<IfModule mod_headers.c>
   Header add Access-Control-Allow-Origin: *
</IfModule>

Allow cross-origin requests from a specific domain

text
<IfModule mod_headers.c>
   Header add Access-Control-Allow-Origin: "https://cdn.hap5.top"
</IfModule>

Allow cross-origin requests from multiple specific domains

text
<IfModule mod_headers.c>
   SetEnvIf Origin "http(s)?://(www.)?(域名1.com|域名2.com)$" AccessControlAllowOrigin=$0$1
   Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
</IfModule>

Comments