A prefect Apache server settings to avoid CORS error for HLS

CK CK 3788 
Created at
Updated at  
603 0 0

A CORS (Cross-Origin Resource Sharing) error occurs when a web page makes a request to a server from a different origin (domain, protocol, or port) than the page itself, and the server doesn't have the appropriate headers configured to allow that request. Essentially, the browser, acting as a security measure, blocks the response from the server to prevent malicious cross-site scripting attacks.

A prefect Apache server settings to avoid CORS error for HLS

To avoid CORS errors when a client sends data in the query string or headers with an Apache server, you need to configure Apache's mod_headers module to add the necessary CORS headers (Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers) to the HTTP responses. These headers specify which origins are allowed to access the resource, the allowed HTTP methods, and the allowed request headers, respectively. The Access-Control-Allow-Origin can be set to a specific origin, a wildcard (*), or dynamically based on the request origin if you require more granular control. Proper configuration of these headers ensures the browser allows the cross-origin request.
 

Below is the verified example to avoid CORS errors that based in .htaccess

# HLS content settings
<FilesMatch "\.(ts|m3u8)$">
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Methods "GET, HEAD, OPTIONS"
        Header set Access-Control-Allow-Headers "Origin, Accept, Content-Type, X-Requested-With, Range, If-None-Match, If-Modified-Since, Cache-Control"
        Header set Access-Control-Allow-Credentials "true"
</FilesMatch>

# Content Types
AddType application/vnd.apple.mpegurl .m3u8
AddType video/mp2t .ts

# M3U8 rewrite
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\.m3u8$ index.m3u8.php?m3u8obj=$1 [L,QSA]

 

You can add the additional custom Headers at Access-Control-Allow-Headers

 

The above case is optimized for HLS. Below is for the general purpose:

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, HEAD, OPTIONS"
Header set Access-Control-Allow-Headers "Origin, Accept, Content-Type, X-Requested-With, Range, If-None-Match, If-Modified-Since, Cache-Control"
Header set Access-Control-Expose-Headers "Content-Length, Content-Range, Content-Type"
Header set Access-Control-Allow-Credentials "true"

# Content Types
AddType application/vnd.apple.mpegurl .m3u8
AddType video/mp2t .ts

# M3U8 rewrite
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\.m3u8$ index.m3u8.php?m3u8obj=$1 [L,QSA]

 

Sometimes AI kindly recommend Header always set, but it could cause overlapped settings, so you will be faced CORS error.

 

If you still face CORS error, I recommend you to add below to /etc/httpd/conf/httpd.conf or /etc/httpd/conf.d/vhost.conf

<Directory "/var/www/html">
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Methods "GET, POST, HEAD, OPTIONS"
        Header set Access-Control-Allow-Headers "Origin, Accept, Authorization, Content-Type, X-Requested-With, Range, If-None-Match, If-Modified-Since, Cache-Control, ODX-User-Agent, odx-user-agent, ODX_User_Agent"
        Header set Access-Control-Expose-Headers "Content-Length, Content-Range, Content-Type"
        Header set Access-Control-Allow-Credentials "true"

        # Allow OPTIONS requests
        RewriteEngine On
        RewriteCond %{REQUEST_METHOD} OPTIONS
        RewriteRule ^(.*)$ $1 [R=200,L]
</Directory>
Tags .htaccess Access-Control-Allow-Headers Access-Control-Allow-Origin CORS CORS Error Cross-Origin Resource Sharing Facebook X
Comments 0
Similar posts
  1. Preventing Unauthorized Image Crawling by Bots: A Clever Discovery and How to Address It
    1,725
  2. When HLS.js need to load m3u8 URL redirecting to the different URL (302 Redirection)
    659
  1. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  2. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  3. From SDI to MXL: Inside the Cloud-Native Infrastructure Powering Next-Gen Live Broadcast
    563
  4. Smooth switching with EXT-X-DISCONTINUITY in HLS
    426
  5. Disabling Audio Tracks in HLS.js
    371
  6. A Deep Dive Into Internet Advertising Ecosystem on Video Streaming Industry
    643
  7. When to use #EXT-X-DISCONTINUITY in HLS?
    1,423
  8. CloudFront Settings for Dynamic HLS Streaming
    650
  9. SafeFrame is a web standard developed by IAB
    505
  10. CSAI vs SSAI
    574
  11. Ad Marker: SCTE-35 Message Encoder based in Python
    794
  12. Ad marker: SCTE-35 enhanced
    570
  13. FFmpeg compile options based on configure
    1,110
  14. Script checking internet restriction with FFmpeg
    678
  15. FFmpeg compile option to support SCTE-35
    837
  16. HTTP Proxy when your URL access is blocked with FFmpeg
    1,886
  17. Limit Maximum Resolution in HLS.js
    663
  18. Magic line when HLS video manifest is mixed with encrypted / unencrypted video chunks
    1,452
  19. Recovering a crashed HLS.js player
    1,274
  20. How do I create animated background for Google Meet?
    19,728
  21. Secure HLS encoding using AES-128 encryption with FFmpeg
    1,130
  22. Machine Learning Models for Ad Detection from a Video Stream
    185
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,728
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    234
  17. The Concept of Scaffolding in AI Agents
    222
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    328
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252