# ============================================
# .htaccess - UNIVERSAL PRODUCTION (DINAMIS SESUAI DOMAIN)
# ============================================

Options -Indexes
Options +FollowSymLinks

RewriteEngine On

# ============================================
# CANONICAL DOMAIN: PAKSA HTTPS + NON-WWW (DINAMIS)
# Otomatis mengikuti domain apa pun tempat web di-deploy
# ============================================
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# ============================================
# IZINKAN AKSES LANGSUNG KE FOLDER ADMIN
# ============================================
RewriteRule ^admin/ - [L]

# ============================================
# REDIRECT index.php KE ROOT (CEGAH DUPLIKAT)
# ============================================
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php [NC]
RewriteRule ^index\.php$ / [R=301,L]

RewriteCond %{QUERY_STRING} ^route=home$ [NC]
RewriteRule ^$ /? [R=301,L]

# ============================================
# HAPUS TRAILING SLASH (KONSISTEN DENGAN CANONICAL)
# ============================================
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteRule ^ /%1 [R=301,L]

# ============================================
# REWRITE SITEMAP, FEED, ROBOTS & DYNAMIC CSS
# ============================================
RewriteRule ^sitemap\.xml$ sitemap.php [L]
RewriteRule ^sitemap-local\.xml$ sitemap-local.php [L]
RewriteRule ^merchant-feed\.xml$ merchant-feed.php [L]
RewriteRule ^local-inventory\.xml$ local-inventory.php [L]
RewriteRule ^robots\.txt$ robots.php [L]
RewriteRule ^assets/style\.css$ assets/style.php [L]

# ============================================
# ROUTING - Single Entry Point
# ============================================
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]

# ============================================
# PROTEKSI FILE SENSITIF
# ============================================
<FilesMatch "\.(env|sql|log|ini|sh|bak|old|htaccess|htpasswd)$">
    Require all denied
</FilesMatch>

# ============================================
# KOMPRESI GZIP
# ============================================
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/xml text/javascript application/javascript application/json
</IfModule>

# ============================================
# CACHE BROWSER
# ============================================
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType text/javascript "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType text/html "access plus 1 hour"
</IfModule>
