# Apache Configuration for Clean URLs

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    # Redirect stream URLs to stream.php
    # Supports: /stream/TOKEN.m3u8 or /TOKEN.m3u8
    RewriteRule ^stream/([a-f0-9]+)\.m3u8$ stream.php [L,QSA]
    RewriteRule ^([a-f0-9]+)\.m3u8$ stream.php [L,QSA]
    
    # Redirect direct stream URLs (TS/MP4/FLV)
    # Supports: /live/TOKEN or /direct/TOKEN
    RewriteRule ^live/([a-f0-9]+)$ direct.php [L,QSA]
    RewriteRule ^direct/([a-f0-9]+)$ direct.php [L,QSA]
    
    # Prevent directory listing
    Options -Indexes
    
    # Protect sensitive files
    <FilesMatch "(config\.php|database\.sql|create_admin\.php)$">
        Order allow,deny
        Deny from all
    </FilesMatch>
</IfModule>

# Set default index
DirectoryIndex index.php index.html

# Enable CORS for streaming
<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header set Access-Control-Allow-Headers "Content-Type"
</IfModule>

# Cache control for segments
<FilesMatch "\.(ts|m3u8)$">
    Header set Cache-Control "max-age=300, public"
</FilesMatch>

# Compress text files
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/vnd.apple.mpegurl
</IfModule>

# Set proper MIME types
<IfModule mod_mime.c>
    AddType application/vnd.apple.mpegurl .m3u8
    AddType video/mp2t .ts
</IfModule>
