# ============================================================
# SADEV Turizm — Apache yapılandırması (public/)
# ============================================================
# Statik asset'lerde sıkıştırma + immutable cache + HSTS +
# yedek dosya engelleme. PHP routing sanal host config'inde
# tanımlı (public/index.php / public/router.php).
# ============================================================

# ----- Rewrite: var olan dosya/klasörleri PHP'den muaf tut -----
# Bazı paylaşımlı hosting sağlayıcıları (cPanel, Plesk) sanal host
# seviyesinde FallbackResource veya RewriteRule ile TÜM istekleri
# index.php'ye yönlendirir. Bu durumda /assets/css/app.min.css gibi
# istekler PHP'ye düşer; router eşleşmediği için 404 view render
# edilir; bazı ortamlarda bu 500'e dönüşür (log tabanlı 404 yakalama
# veya 404 view'da undefined variable). Var olan dosyaları erken
# çıkışla PHP'den muaf tutuyoruz.
DirectoryIndex index.php

# ----- Belge yükleme sınırları (yalnızca Apache mod_php; PHP-FPM'de public/.user.ini geçerlidir) -----
<IfModule mod_php.c>
    php_value upload_max_filesize 32M
    php_value post_max_size 64M
    php_value max_file_uploads 20
    php_value max_execution_time 120
    php_value memory_limit 256M
</IfModule>
<IfModule mod_php7.c>
    php_value upload_max_filesize 32M
    php_value post_max_size 64M
    php_value max_file_uploads 20
    php_value max_execution_time 120
    php_value memory_limit 256M
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Authorization başlığını PHP'ye ilet
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # robots.txt, sitemap.xml ve favicon panel ayarlarına göre PHP'den üretilir (diskte dosya olsa bile)
    RewriteRule ^(robots\.txt|sitemap\.xml|sitemap\.xsl|llms\.txt|llms-full\.txt|llm\.txt|favicon\.ico)$ index.php [L,QSA]
    # Eski (Murat Zeytin Tarım) projeden kalan dosyalar yayınlanmaz
    RewriteRule ^(ads\.txt|googlec06e7036d86e9437\.html|opcache_reset\.php)$ - [R=404,L]
    RewriteRule ^(assets/(images|videos|cache)|uploads/(products|categories|blog))/ - [R=404,L]

    # /admin gerçek bir klasör (public/admin): klasör kontrolünden ÖNCE panele verilir. Aksi halde Apache
    # (DirectorySlash) kök .htaccess'in iç yönlendirmesini dışarı sızdırıp 301 ile /public/admin/ adresine atar → döngü.
    RewriteRule ^admin/?$ admin/index.php [L,QSA]

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # Front controller: yönetim paneli ve site (sanal host'ta tanımlı değilse de çalışır)
    RewriteRule ^admin(/.*)?$ admin/index.php [L,QSA]
    RewriteRule ^ index.php [L,QSA]
</IfModule>

# ----- GZIP sıkıştırma (mod_deflate) -----
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/plain text/xml text/css
    AddOutputFilterByType DEFLATE application/javascript application/json application/xml
    AddOutputFilterByType DEFLATE application/atom+xml application/rss+xml
    AddOutputFilterByType DEFLATE image/svg+xml font/woff font/woff2
</IfModule>

# ----- Brotli (mod_brotli yüklüyse deflate'tan daha iyi) -----
<IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI_COMPRESS text/html
    AddOutputFilterByType BROTLI_COMPRESS text/plain text/xml text/css
    AddOutputFilterByType BROTLI_COMPRESS application/javascript application/json
    AddOutputFilterByType BROTLI_COMPRESS image/svg+xml
</IfModule>

# ----- Cache + güvenlik header'ları -----
<IfModule mod_headers.c>
    # Statik asset'ler 1 yıl immutable cache
    <FilesMatch "\.(js|css|woff2?|ttf|otf|eot|svg|png|jpg|jpeg|gif|webp|ico)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
    # HTML kısa cache + must-revalidate
    <FilesMatch "\.(html|htm)$">
        Header set Cache-Control "public, max-age=3600, must-revalidate"
    </FilesMatch>

    # HSTS — 2 yıl, subdomain + preload uyumlu
    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
    # Güvenlik header'ları
    Header always set X-Content-Type-Options "nosniff"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
</IfModule>

# ----- Yedek / eski dosyaları dışarıya kapat -----
# Üretimde kullanılmayan .bak/.backup dosyaları Apache üzerinden
# erişilemesin. .gitignore ile birlikte savunma-derinliği sağlar.
<FilesMatch "\.(bak|backup|bak_[0-9]+|backup[0-9]*|tmp|log|sql|dump|swp|swo)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

# ----- Gizli dosyaları gizle -----
<FilesMatch "^\.">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>
