RewriteEngine On

# Redirect to HTTPS (optional, uncomment if you have SSL)
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Remove www (optional, uncomment if needed)
# RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
# RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Handle admin routes
RewriteRule ^admin/?$ /admin/dashboard [R=301,L]
RewriteRule ^admin/dashboard/?$ index.php [L]
RewriteRule ^admin/login/?$ index.php [L]
RewriteRule ^admin/logout/?$ index.php [L]
RewriteRule ^admin/services/?$ index.php [L]
RewriteRule ^admin/gallery/?$ index.php [L]
RewriteRule ^admin/messages/?$ index.php [L]
RewriteRule ^admin/testimonials/?$ index.php [L]
RewriteRule ^admin/pages/?$ index.php [L]
RewriteRule ^admin/settings/?$ index.php [L]

# Handle frontend routes
RewriteRule ^about/?$ index.php [L]
RewriteRule ^hakkimizda/?$ index.php [L]
RewriteRule ^services/?$ index.php [L]
RewriteRule ^hizmetler/?$ index.php [L]
RewriteRule ^gallery/?$ index.php [L]
RewriteRule ^galeri/?$ index.php [L]
RewriteRule ^contact/?$ index.php [L]
RewriteRule ^iletisim/?$ index.php [L]

# Handle dynamic pages
RewriteRule ^page/([a-zA-Z0-9-_]+)/?$ index.php [L]

# Security: Block access to sensitive files
<Files "*.sql">
    Order Allow,Deny
    Deny from all
</Files>

<Files "*.log">
    Order Allow,Deny
    Deny from all
</Files>

<Files ".env">
    Order Allow,Deny
    Deny from all
</Files>

# Block access to config and includes directories
RewriteRule ^config/ - [F,L]
RewriteRule ^includes/ - [F,L]
RewriteRule ^models/ - [F,L]
RewriteRule ^controllers/ - [F,L]

# Allow access to uploads directory
RewriteRule ^uploads/ - [L]

# Set default charset
AddDefaultCharset UTF-8

# Enable compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

# Set cache headers for static files
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 2 days"
</IfModule>
