# .htaccess - لتشغيل Next.js على cPanel (Apache)

RewriteEngine On

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

# Block .env and sensitive files
<FilesMatch "^\.env">
  Order allow,deny
  Deny from all
</FilesMatch>

<FilesMatch "\.(json|lock|log|md|yml|yaml)$">
  Order allow,deny
  Deny from all
</FilesMatch>

# Next.js routing - serve static files directly
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Handle client-side routing - send all other requests to index.html
RewriteRule ^ index.html [L]

# Security headers
<IfModule mod_headers.c>
  Header set X-Content-Type-Options "nosniff"
  Header set X-Frame-Options "SAMEORIGIN"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
  Header set Permissions-Policy "geolocation=(), microphone=(), camera=()"

  # Enable gzip
  <IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json text/plain text/xml application/xml application/xml+rss text/javascript
  </IfModule>

  # Cache static assets
  <FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|webp|svg|woff|woff2|ttf|eot)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
</IfModule>

# Disable server signature
ServerSignature Off

# Custom error pages
ErrorDocument 404 /404.html
