# televiziebi.online - Apache .htaccess
# SSL/HTTPS and www redirect handled by Cloudflare

# Default charset for Indonesian content
AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Redirect /index.php to / (canonical URL)
    RewriteCond %{THE_REQUEST} \s/index\.php(\?[^\s]*)?\s [NC]
    RewriteRule ^index\.php$ / [R=301,L,QSA]

    # Remove trailing slash (except for directories)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [R=301,L]

    # Static assets and real files - pass through
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # Directory requests - pass through
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # Strip .php extension (redirect to clean URL)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)\.php$ $1 [R=301,L]

    # Route clean URLs: /services, /about, /contact, etc.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA]

    # Root URL
    RewriteRule ^$ index.php?page=home [L,QSA]
</IfModule>

# Security Headers
<IfModule mod_headers.c>
    # Prevent clickjacking
    Header set X-Frame-Options "SAMEORIGIN"
    
    # XSS Protection
    Header set X-XSS-Protection "1; mode=block"
    
    # Prevent MIME type sniffing
    Header set X-Content-Type-Options "nosniff"
    
    # Referrer Policy
    Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# Enable Compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

# Browser Caching
<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 image/webp "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/html "access plus 0 seconds"
</IfModule>

# Prevent Directory Browsing
Options -Indexes

# Custom Error Pages
ErrorDocument 404 index.php?page=home

# PHP Settings (if mod_php is enabled)
<IfModule mod_php8.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>
