# Enable rewrite engine
RewriteEngine On

# Prevent directory listing
Options -Indexes

# Set environment-aware base path
<IfModule mod_env.c>
    SetEnv APPLICATION_ENV development
</IfModule>

# Error pages - works for both local and production
ErrorDocument 404 /404.php
ErrorDocument 403 /404.php
ErrorDocument 500 /404.php

# 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"
</IfModule>

# Protect sensitive files
<FilesMatch "^(config|database|\.git|\.env)">
    Order allow,deny
    Deny from all
</FilesMatch>

# Cache static resources
<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 text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>

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

# Force HTTPS in production (uncomment when deploying)
# <IfModule mod_rewrite.c>
#     RewriteCond %{HTTPS} off
#     RewriteCond %{HTTP_HOST} !^localhost [NC]
#     RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# </IfModule>

# Prevent access to hidden files
<FilesMatch "^\.(htaccess|htpasswd|gitignore|env)">
    Order allow,deny
    Deny from all
</FilesMatch>

# PHP security settings
<IfModule mod_php7.c>
    php_flag display_errors Off
    php_flag log_errors On
    php_value error_log /tmp/php_errors.log
</IfModule>
