# PHP Live! - Modern .htaccess for PHP 8.2+
# Enable rewrite engine
RewriteEngine On

# Force HTTPS (uncomment if using SSL)
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

# PHP settings for PHP 8.2
<IfModule mod_php8.c>
    php_value max_execution_time 300
    php_value memory_limit 256M
    php_value post_max_size 64M
    php_value upload_max_filesize 64M
    php_value max_input_vars 3000
    php_value session.gc_maxlifetime 1440
    php_value session.cookie_lifetime 0
    php_flag session.use_strict_mode On
    php_flag session.use_cookies On
    php_flag session.use_only_cookies On
    php_flag session.cookie_httponly On
    php_flag session.cookie_samesite "Lax"
</IfModule>

# Fallback for older PHP versions
<IfModule mod_php7.c>
    php_value max_execution_time 300
    php_value memory_limit 256M
    php_value post_max_size 64M
    php_value upload_max_filesize 64M
    php_value max_input_vars 3000
    php_value session.gc_maxlifetime 1440
    php_value session.cookie_lifetime 0
    php_flag session.use_strict_mode On
    php_flag session.use_cookies On
    php_flag session.use_only_cookies On
    php_flag session.cookie_httponly On
</IfModule>

# Cache control for static assets
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 2 days"
</IfModule>

# Gzip compression
<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
</IfModule>

# Prevent access to sensitive files
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|inc|bak)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# Prevent access to config files
<FilesMatch "^(config|database|\.env)">
    Order Allow,Deny
    Deny from all
</FilesMatch>