# ============================================================
# Online Course Selling Platform - .htaccess
# ============================================================

# Enable Rewrite Engine
<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Force HTTPS (uncomment in production)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

    # Remove www (uncomment in production)
    # RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    # RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # SEO Friendly URLs
    RewriteRule ^course/([a-zA-Z0-9_-]+)$ course.php?slug=$1 [L,QSA]
    RewriteRule ^lesson/([0-9]+)$ lesson.php?id=$1 [L,QSA]

    # Protect sensitive files
    RewriteRule ^includes/ - [F,L]
    RewriteRule ^admin/partials/ - [F,L]
    RewriteRule ^database\.sql$ - [F,L]
    RewriteRule ^error\.log$ - [F,L]
    RewriteRule ^install\.html$ - [F,L]
    
    # Prevent directory listing
    Options -Indexes
</IfModule>

# ============================================================
# Security Headers
# ============================================================
<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# ============================================================
# Caching
# ============================================================
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/html "access plus 1 day"
</IfModule>

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

# ============================================================
# PHP Settings
# ============================================================
<IfModule mod_php.c>
    php_value upload_max_filesize 50M
    php_value post_max_size 55M
    php_value max_execution_time 300
    php_value max_input_time 300
    php_flag display_errors off
    php_flag log_errors on
</IfModule>

# ============================================================
# Error Pages
# ============================================================
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 500 /500.php

# ============================================================
# Block Bad Bots
# ============================================================
<IfModule mod_rewrite.c>
    RewriteCond %{HTTP_USER_AGENT} ^.*(bot|crawl|spider|scrape|curl|wget|python).* [NC]
    RewriteRule .* - [F,L]
</IfModule>