RewriteEngine On
RewriteBase /elzem-player/

# Don't rewrite existing files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Handle all channel URLs (with or without stream- prefix)
RewriteRule ^(.+)$ player.php?id=$1 [QSA,L]

# Catch-all rule
RewriteRule ^.*$ player.php [QSA,L]

# =============================================================================
# CACHING & PERFORMANCE
# =============================================================================

# Enable compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

# Browser caching
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/ico "access plus 1 month"
</IfModule>

# =============================================================================
# BLOCK MALICIOUS REQUESTS & BOTS
# =============================================================================

# Block common attack patterns
<IfModule mod_rewrite.c>
    # Block SQL injection attempts
    RewriteCond %{QUERY_STRING} (union|select|insert|delete|drop|create|alter) [NC,OR]
    RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
    RewriteCond %{QUERY_STRING} (javascript:|vbscript:|onload|onerror) [NC,OR]
    RewriteCond %{QUERY_STRING} (base64_encode|base64_decode|GLOBALS|REQUEST) [NC,OR]
    RewriteCond %{QUERY_STRING} (<|%3C).*iframe.*(>|%3E) [NC,OR]
    RewriteCond %{QUERY_STRING} (eval\(|assert\(|passthru|exec|system) [NC]
    RewriteRule .* - [F,L]
    
    # Block common exploit attempts
    RewriteCond %{REQUEST_URI} (wp-admin|wp-login|wp-config|phpMyAdmin|administrator) [NC,OR]
    RewriteCond %{REQUEST_URI} (\.php\.|\.asp\.|\.jsp\.|\.cgi\.) [NC,OR]
    RewriteCond %{REQUEST_URI} (proc/self/environ|/etc/passwd|cmd\.exe) [NC,OR]
    RewriteCond %{REQUEST_URI} (MSADC|Shell|Proxy|webdav) [NC]
    RewriteRule .* - [F,L]
</IfModule>

# Block suspicious user agents
<IfModule mod_rewrite.c>
    RewriteCond %{HTTP_USER_AGENT} (sqlmap|nikto|wget|curl|libwww|python|perl|scan|java|winhttp|clshttp|loader) [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} (harvest|extract|grab|suck|spider|bot|crawler) [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} (VideoDownloadHelper|FlashGet|GetRight|GetWeb|Go!Zilla|Go-Ahead-Got-It|GrabIt) [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} (GetSmart|GetZilla|InterGET|Internet\ Ninja|JetCar|Mass\ Downloader|MIDown) [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} (^$|^-$) [NC]
    RewriteRule .* - [F,L]
</IfModule>

<FilesMatch "\.json$">
    Order allow,deny
    Deny from all
</FilesMatch>

# =============================================================================
# URL REWRITING RULES
# =============================================================================

# Don't rewrite existing files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Handle token-based streaming (encrypted URLs)
RewriteCond %{QUERY_STRING} ^elzem_stream=1&token=([a-zA-Z0-9+/=_-]+)$ [NC]
RewriteRule ^.*$ index.php?elzem_stream=1&token=%1 [QSA,L]

# Handle AJAX requests for URL signing
RewriteRule ^ajax-sign-url/?$ index.php [QSA,L]

# Channel URLs with permalink structure
# Examples: /stream-1, /stream-channel-name, /stream-123
RewriteRule ^stream-([a-zA-Z0-9_-]+)/?$ index.php [QSA,L]

# Legacy support for ?id= parameter
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^.*$ index.php?id=%1 [QSA,L]

# Admin panel access
RewriteRule ^admin/?$ admin.php [QSA,L]

# Player direct access (for iframe embedding)
RewriteRule ^player/?$ player.php [QSA,L]

# API endpoints for future use
RewriteRule ^api/([a-zA-Z0-9_-]+)/?$ api.php?endpoint=%1 [QSA,L]

# Catch-all rule - send everything else to index.php
RewriteRule ^.*$ index.php [QSA,L]

RewriteEngine On
RewriteBase /elzem-player/

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^stream-(.+)$ index.php [QSA,L]
RewriteRule ^.*$ index.php [QSA,L]

# =============================================================================
# UNICODE/ARABIC URL SUPPORT
# =============================================================================

