<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

# Security: Prevent access to storage directory if accidentally created as folder
<IfModule mod_rewrite.c>
    # Allow access to storage folder contents (images, etc.)
    RewriteCond %{REQUEST_URI} ^/storage/
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^storage/(.*)$ storage/$1 [L]
</IfModule>

# File caching and compression for better performance
<IfModule mod_expires.c>
    ExpiresActive on
    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/webp "access plus 1 month"
    ExpiresByType video/mp4 "access plus 1 week"
    ExpiresByType video/avi "access plus 1 week"
    ExpiresByType video/mov "access plus 1 week"
    ExpiresByType video/wmv "access plus 1 week"
</IfModule>

# Enable range requests for video streaming (progressive download)
<IfModule mod_headers.c>
    # Allow range requests for video files
    <FilesMatch "\.(mp4|avi|mov|wmv|webm|mkv)$">
        Header always set Accept-Ranges bytes
        Header always set Cache-Control "public, max-age=604800"
        # Disable buffering for video files to enable proper streaming
        Header always set X-Accel-Buffering "no"
    </FilesMatch>
    
    # Enable CORS for video streaming if needed
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "GET, POST, OPTIONS, HEAD"
    Header always set Access-Control-Allow-Headers "Range, Content-Range, Content-Type"
</IfModule>

# Optimize video delivery
<IfModule mod_mime.c>
    AddType video/mp4 .mp4
    AddType video/avi .avi
    AddType video/quicktime .mov
    AddType video/x-ms-wmv .wmv
    AddType video/webm .webm
    AddType video/x-matroska .mkv
</IfModule>

# PHP configuration for better video streaming
<IfModule mod_php.c>
    # Increase limits for video streaming
    php_value max_execution_time 0
    php_value memory_limit 512M
    php_value output_buffering Off
    php_value zlib.output_compression Off
</IfModule>

# Apache configuration for video streaming
<IfModule mod_setenvif.c>
    # Set environment for video files
    SetEnvIf Request_URI "\.(mp4|avi|mov|wmv|webm|mkv)$" no-gzip dont-vary
</IfModule>

# Gzip compression (but exclude video files)
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    
    # Exclude video files from compression
    SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png|mp4|avi|mov|wmv|webm|mkv)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \
        \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
</IfModule>

# Video Streaming Optimizations for Hosting
<IfModule mod_headers.c>
    # Enable Range Requests for all video files
    <FilesMatch "\.(mp4|avi|mov|wmv|webm|mkv|flv|m4v|3gp|ogv)$">
        Header set Accept-Ranges bytes
        Header set Cache-Control "public, max-age=3600"
        Header set Connection keep-alive
        Header unset ETag
        Header unset Last-Modified
        
        # Disable server-side compression for video files
        Header unset Content-Encoding
        Header set X-Accel-Buffering no
        
        # CORS headers for video streaming
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Methods "GET, HEAD, OPTIONS"
        Header set Access-Control-Allow-Headers "Range, Content-Range, Accept-Ranges"
        Header set Access-Control-Expose-Headers "Content-Range, Accept-Ranges, Content-Length"
    </FilesMatch>
</IfModule>

# PHP Settings for Video Streaming
<IfModule mod_php.c>
    # Increase execution time for video streaming
    php_value max_execution_time 0
    php_value memory_limit 256M
    php_value max_input_time 0
    
    # Disable output buffering for streaming
    php_value output_buffering Off
    php_value zlib.output_compression Off
    php_value implicit_flush On
    
    # File upload settings for video uploads
    php_value upload_max_filesize 1024M
    php_value post_max_size 1024M
    php_value max_file_uploads 20
    
    # Session settings
    php_value session.cookie_httponly 1
    php_value session.use_only_cookies 1
</IfModule>

# Alternative PHP Settings (for servers that don't support mod_php)
<IfModule mod_fcgid.c>
    FcgidIOTimeout 3600
    FcgidConnectTimeout 3600
    FcgidBusyTimeout 3600
    FcgidIdleTimeout 3600
</IfModule>

# Gzip Compression (but exclude video files)
<IfModule mod_deflate.c>
    # Compress text files
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    
    # Exclude video files from compression
    SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png|mp4|avi|mov|wmv|webm|mkv|flv|m4v|3gp|ogv)$ no-gzip dont-vary
</IfModule>

# Expires Headers for Performance
<IfModule mod_expires.c>
    ExpiresActive On
    
    # Video files - cache for 1 hour
    ExpiresByType video/mp4 "access plus 1 hour"
    ExpiresByType video/avi "access plus 1 hour"
    ExpiresByType video/quicktime "access plus 1 hour"
    ExpiresByType video/x-ms-wmv "access plus 1 hour"
    ExpiresByType video/webm "access plus 1 hour"
    ExpiresByType video/x-matroska "access plus 1 hour"
    
    # Images - cache for 1 week
    ExpiresByType image/jpg "access plus 1 week"
    ExpiresByType image/jpeg "access plus 1 week"
    ExpiresByType image/gif "access plus 1 week"
    ExpiresByType image/png "access plus 1 week"
    ExpiresByType image/webp "access plus 1 week"
    
    # CSS and JavaScript - cache for 1 month
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
</IfModule>

# Security Headers


# Prevent access to sensitive files
<FilesMatch "^(\.|composer\.|package\.|webpack\.|artisan$|\.env)">
    Order allow,deny
    Deny from all
</FilesMatch>

# Optimize for shared hosting environments
<IfModule mod_dir.c>
    DirectoryIndex index.php index.html
</IfModule>

# Handle large file uploads for video content
<IfModule mod_limitipconn.c>
    # Allow more connections for video streaming
    MaxConnPerIP 10
</IfModule>

# Error handling
ErrorDocument 404 /index.php
ErrorDocument 500 /index.php
