# File: .htaccess di root (public_html)

# Aktifkan rewrite engine
RewriteEngine On

# Redirect www ke non-www (pilih salah satu)
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

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

# Hapus trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

# Custom Error Pages
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php

# Security Headers untuk SEO & Security
<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"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
    
    # Caching untuk SEO
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|webp)$">
        Header set Cache-Control "max-age=31536000, public"
    </FilesMatch>
</IfModule>

# Kompresi GZIP untuk kecepatan (SEO penting!)
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript application/json
</IfModule>

# URL Rewriting untuk SEO Friendly URLs
# Contoh: ubah product.php?id=123 menjadi product/nama-produk

# Untuk halaman produk
RewriteRule ^produk/([a-zA-Z0-9-]+)/?$ product.php?slug=$1 [QSA,L,NC]

# Untuk halaman blog
RewriteRule ^blog/([a-zA-Z0-9-]+)/?$ blog.php?slug=$1 [QSA,L,NC]

# Untuk halaman kategori
RewriteRule ^kategori/([a-zA-Z0-9-]+)/?$ category.php?slug=$1 [QSA,L,NC]

# Blok akses ke file sensitif
<FilesMatch "\.(env|log|ini|sql|bak|inc|config)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Nonaktifkan directory browsing (SEO & Security)
Options -Indexes

# Prevent hotlinking images
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?domain-anda\.com/.*$ [NC]
RewriteRule \.(jpg|jpeg|png|gif|webp)$ - [F,NC]