# Prevent directory listing
Options -Indexes

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

# Protect upload directories
<FilesMatch "\.(php|php5|phtml)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Enable URL rewriting
<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Redirect to HTTPS (uncomment if you have SSL)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # Remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ /$1 [L,R=301]
    
    # Block access to hidden files
    RewriteCond %{REQUEST_URI} "/\."
    RewriteRule ^(.*)$ - [F,L]
</IfModule>

# Set default character set
AddDefaultCharset UTF-8

# Limit file upload size
php_value upload_max_filesize 5M
php_value post_max_size 5M

# Protect against script execution in uploads directory
<Directory "uploads">
    <FilesMatch "\.(php|php5|phtml|cgi|pl|py|jsp|asp|sh|cgi)$">
        Order allow,deny
        Deny from all
    </FilesMatch>
</Directory>

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

# Set cache headers for static files
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</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"
</IfModule>
