# ====================================================================
# DigiEchelon - Hostinger Apache / PHP 8 .htaccess Configuration
# Clean URLs, Security, Compression, Caching, and Error Handling
# ====================================================================

# Prevent directory browsing and disable MultiViews negotiation conflict
Options -Indexes -MultiViews +FollowSymLinks

# Set default directory index file
DirectoryIndex index.php index.html

RewriteEngine On
RewriteBase /

# Ensure UTF-8 Charset
AddDefaultCharset UTF-8
AddCharset UTF-8 .html .css .js .xml .json .rss

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

# Custom 404 Error Document
ErrorDocument 404 /404.php

# Gzip Deflate Compression for Core Web Vitals & Speed
<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
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# Browser Caching Headers
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType image/webp "access 1 year"
    ExpiresByType image/svg+xml "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType application/javascript "access 1 month"
    ExpiresByType text/javascript "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 2 days"
</IfModule>

# ====================================================================
# CLEAN URL REWRITE RULES
# ====================================================================

# 1. If an actual file exists, serve it directly
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# 2. If an actual directory with index.php exists, serve it directly
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^ - [L]

# 3. Clean service URLs: Map /service-name/ or /service-name to service-name.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ $1.php [L,QSA]

# 4. Fallback for subdirectories: Map /service-name/ to service-name/index.php
RewriteCond %{DOCUMENT_ROOT}/$1/index.php -f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ $1/index.php [L,QSA]

# 5. Dedicated sub-service clean URLs: Map /category-slug/subservice-slug to subservice.php?category=$1&sub=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ subservice.php?category=$1&sub=$2 [L,QSA]

# 6. Route non-existent files to 404 handler
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ 404.php [L]
