# Apache URL Rewriting - Uzantı Gizleme ve Yönlendirme
# .php ve .html uzantılarını gizler ve otomatik yönlendirme yapar
# Örnek: darqsmm.com/login.php → darqsmm.com/login (301 redirect)

# Rewrite Engine'i Aktif Et
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # .php uzantılı URL'leri uzantısız versiyona yönlendir (301 Permanent Redirect)
    # Örnek: /login.php → /login
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^ /%1? [R=301,L]

    # .html uzantılı URL'leri uzantısız versiyona yönlendir (301 Permanent Redirect)
    # Örnek: /index.html → /index
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
    RewriteRule ^ /%1? [R=301,L]

    # Eğer istek bir dizin veya dosya ise, rewrite yapma
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    # .php uzantısını gizle (iç rewrite)
    # Örnek: /login → /login.php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php [L,QSA]

    # .html uzantısını gizle (iç rewrite)
    # Örnek: /index → /index.html
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)$ $1.html [L,QSA]

    # Eğer dosya bulunamazsa 404 döndür
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ error.php?code=404 [L]
</IfModule>

# Güvenlik Ayarları
# Hassas dosyalara erişimi engelle
<FilesMatch "^(config\.php|\.htaccess|\.git|composer\.(json|lock)|\.env|\.sql)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>


# Güvenlik Headers - Bilgi Sızıntısını Önle
<IfModule mod_headers.c>
    # Server bilgisini gizle
    Header unset Server
    Header unset X-Powered-By
    
    # X-Frame-Options
    Header always set X-Frame-Options "SAMEORIGIN"
    
    # X-Content-Type-Options
    Header always set X-Content-Type-Options "nosniff"
    
    # X-XSS-Protection
    Header always set X-XSS-Protection "1; mode=block"
    
    # Referrer Policy
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # IP adresini header'dan gizle
    Header unset X-Forwarded-For
    Header unset X-Real-IP
</IfModule>

# PHP Ayarları (Opsiyonel)
<IfModule mod_php7.c>
    php_value upload_max_filesize 10M
    php_value post_max_size 10M
    php_value max_execution_time 300
    php_value max_input_time 300
</IfModule>

# Hata Sayfaları (Opsiyonel)
ErrorDocument 404 /error.php?code=404
ErrorDocument 403 /error.php?code=403
ErrorDocument 500 /error.php?code=500

# Gzip Sıkıştırma (Performans için)
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

# Tarayıcı Önbellekleme (Performans için)
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>
# PHP Güvenlik Ayarları
<IfModule mod_php7.c>
    # Hata mesajlarını gizle
    php_flag display_errors Off
    php_flag display_startup_errors Off
    php_value error_reporting 0
    
    # Güvenlik ayarları
    php_flag expose_php Off
    php_value session.cookie_httponly 1
    php_value session.cookie_secure 1
    php_value session.use_only_cookies 1
</IfModule>

# Dosya Erişim Koruması
# SQL, config ve diğer hassas dosyaları engelle
<FilesMatch "\.(sql|log|ini|conf|bak|backup|old|tmp|temp)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>