# ============================================================
# EverMarkit — Apache Configuration (.htaccess)
# Optimised for cPanel / Shared Hosting
# ============================================================

Options -Indexes
Options +FollowSymLinks

# ── Rewrite Engine ───────────────────────────────────────────
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

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

    # Force www (choose one — comment out if you prefer non-www)
    # RewriteCond %{HTTP_HOST} !^www\. [NC]
    # RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Remove trailing slash from non-directory URLs
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Clean URL extensions (optional)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+)$ $1.html [L]
</IfModule>

# ── Security Headers ─────────────────────────────────────────
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"

    # HSTS (only after HTTPS confirmed working)
    # Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"

    # Remove server signature
    Header unset Server
    Header always unset X-Powered-By
</IfModule>

# ── Protect sensitive files ───────────────────────────────────
<FilesMatch "\.(env|log|sql|bak|config|sh|htpasswd)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# Protect .htaccess itself
<Files .htaccess>
    Order Allow,Deny
    Deny from all
</Files>

# ── MIME Types ────────────────────────────────────────────────
<IfModule mod_mime.c>
    AddType text/html                   .html .htm
    AddType text/css                    .css
    AddType application/javascript      .js
    AddType image/svg+xml               .svg .svgz
    AddType image/webp                  .webp
    AddType font/woff2                  .woff2
    AddType font/woff                   .woff
    AddType application/json            .json
</IfModule>

# ── Compression ───────────────────────────────────────────────
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css
    AddOutputFilterByType DEFLATE application/javascript application/json
    AddOutputFilterByType DEFLATE image/svg+xml font/woff font/woff2
</IfModule>

# ── Browser Caching ───────────────────────────────────────────
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault                          "access plus 1 month"
    ExpiresByType text/html                 "access plus 1 hour"
    ExpiresByType text/css                  "access plus 1 year"
    ExpiresByType application/javascript    "access plus 1 year"
    ExpiresByType image/svg+xml             "access plus 1 year"
    ExpiresByType image/webp                "access plus 1 year"
    ExpiresByType font/woff2                "access plus 1 year"
</IfModule>

# ── Custom Error Pages ────────────────────────────────────────
ErrorDocument 404 /404.html
ErrorDocument 500 /index.html

# ── Block bad bots ────────────────────────────────────────────
<IfModule mod_rewrite.c>
    RewriteCond %{HTTP_USER_AGENT} (bot|crawl|slurp|spider|scan|hack|inject) [NC]
    RewriteCond %{HTTP_USER_AGENT} !(Googlebot|bingbot|DuckDuckBot|Baiduspider|YandexBot|facebot|Applebot) [NC]
    RewriteRule .* - [F,L]
</IfModule>
