# Enable rewrite engine
RewriteEngine On
# Set base directory
RewriteBase /
# Force HTTPS (uncomment in production)
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Protect sensitive directories
RewriteRule ^(includes|config|templates|temp)/ - [F,L]
# Protect download directory
RewriteRule ^downloads/ - [F,L]
# Redirect API requests
RewriteRule ^api/(.*)$ api/index.php?endpoint=$1 [QSA,L]
# Pretty URLs for user pages
RewriteRule ^login$ users/login.php [L]
RewriteRule ^register$ users/register.php [L]
RewriteRule ^dashboard$ users/dashboard.php [L]
RewriteRule ^logout$ users/logout.php [L]
RewriteRule ^profile$ users/profile.php [L]
RewriteRule ^upgrade$ users/upgrade.php [L]
# Admin routes
RewriteRule ^admin$ admin/index.php [L]
RewriteRule ^admin/(.*)$ admin/$1 [L]
# Download route
RewriteRule ^download$ download.php [L]
# Error documents
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 500 /500.php
# Security headers
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"
# Compression
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
# Cache control
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"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
# Prevent access to .htaccess
Order allow,deny
Deny from all
# Prevent access to sensitive files
Order allow,deny
Deny from all
# Protect PHP files in includes directory
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Order Allow,Deny
Allow from all