# ==========================================================
#  Digiturk Bayi Sitesi — Apache yapılandırması
#  public_html/.htaccess olarak yükleyin
# ==========================================================

# ----------------------------------------------------------
# 1) Hassas dosyaları tamamen kapat
# ----------------------------------------------------------
<FilesMatch "^(config\.php|config\.example\.php|\.token_cache\.json|KURULUM\.md|\.env|composer\.(json|lock))$">
    Require all denied
</FilesMatch>

# Hata kayıtları — sunucuda kalsın ama web'den okunmasın
<FilesMatch "(^error_log$|\.log$)">
    Require all denied
</FilesMatch>

# Nokta ile başlayan tüm gizli dosyalar
<FilesMatch "^\.">
    Require all denied
</FilesMatch>

# ----------------------------------------------------------
# 2) Dizin listelemeyi kapat
# ----------------------------------------------------------
Options -Indexes

# ----------------------------------------------------------
# 3) HTTPS'e yönlendir
# ----------------------------------------------------------
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

    # ------------------------------------------------------
    # 3b) Temiz URL'ler — /basvuru-uydu gibi uzantısız adresler
    # ------------------------------------------------------

    # index.html → site kökü  (genel kuraldan ÖNCE gelmeli)
    RewriteCond %{THE_REQUEST} \s/+index\.html[\s?] [NC]
    RewriteRule ^ / [R=301,L]

    # Diğer eski .html adresleri kalıcı olarak uzantısıza yönlendir
    # (arama motorlarındaki mevcut bağlantılar korunur)
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
    RewriteRule ^ /%1 [R=301,L]

    # Uzantısız isteği ilgili .html dosyasına eşle
    # RewriteBase, alt dizinde barındırmada yolun doğru çözülmesini sağlar
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.html -f
    RewriteRule ^(.+?)/?$ $1.html [L]
</IfModule>

# ----------------------------------------------------------
# 3c) mod_rewrite yoksa yedek çözüm
#     MultiViews, /basvuru-kutusuz isteğini basvuru-kutusuz.html
#     dosyasıyla kendiliğinden eşler.
# ----------------------------------------------------------
<IfModule mod_negotiation.c>
    Options +MultiViews
</IfModule>

# ----------------------------------------------------------
# 3d) Teşhis işareti — htaccess-test.php bunu okur
#     (zararsızdır, kalabilir)
# ----------------------------------------------------------
<IfModule mod_env.c>
    SetEnv HTACCESS_OK 1
</IfModule>

# ----------------------------------------------------------
# 4) Güvenlik başlıkları
# ----------------------------------------------------------
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "geolocation=(self), microphone=(), camera=(), payment=()"

    # HTTPS zorunluluğu tarayıcıda hatırlansın (1 yıl)
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

    # İçerik güvenlik politikası
    # frame-src: SMS doğrulama sayfası iframe içinde açılıyor
    # 'unsafe-inline': sayfa içi <script>/<style> blokları için gerekli
    Header always set Content-Security-Policy "default-src 'self'; \
script-src 'self' 'unsafe-inline' https://www.googletagmanager.com https://www.google-analytics.com https://mc.yandex.ru https://connect.facebook.net; \
style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; \
font-src 'self' https://fonts.gstatic.com data:; \
img-src 'self' data: https:; \
connect-src 'self' https://nominatim.openstreetmap.org https://www.google-analytics.com https://mc.yandex.ru; \
frame-src 'self' https://digiturkonay.com.tr https://*.digiturkonay.com.tr; \
form-action 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'self'"

    # Sunucu/PHP sürümünü gizle
    Header always unset X-Powered-By
    Header unset Server

    # API yanıtları önbelleğe alınmasın
    <FilesMatch "\.php$">
        Header set Cache-Control "no-store, no-cache, must-revalidate"
    </FilesMatch>
</IfModule>

# ----------------------------------------------------------
# 5) Statik dosya önbelleği
# ----------------------------------------------------------
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/png        "access plus 1 month"
    ExpiresByType image/webp       "access plus 1 month"
    ExpiresByType image/jpeg       "access plus 1 month"
    ExpiresByType image/svg+xml    "access plus 1 month"
    ExpiresByType text/css         "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType text/html        "access plus 0 seconds"
</IfModule>

# ----------------------------------------------------------
# 6) Sıkıştırma
# ----------------------------------------------------------
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css text/plain
    AddOutputFilterByType DEFLATE application/javascript application/json
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# ----------------------------------------------------------
# 7) Varsayılan sayfa ve karakter seti
# ----------------------------------------------------------
DirectoryIndex index.html index.php
AddDefaultCharset UTF-8
