Options -Indexes +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /

# Preserve /.well-known for SSL/Certbot
RewriteRule ^\.well-known/ - [L]

# Block direct access to internal app directory and executable scripts in uploads
RewriteRule ^app/ - [F,L]
RewriteRule ^uploads/.*\.(php|phtml|phar|cgi|pl|sh)$ - [F,L,NC]

# ----------------------------------------------------------------------
# Optional HTTPS Redirection (Uncomment on Production Server with SSL)
# ----------------------------------------------------------------------
# RewriteCond %{HTTPS} off
# RewriteCond %{HTTP_HOST} !^(localhost|127\.0\.0\.1) [NC]
# RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Dynamic XML Sitemap, Robots.txt, PWA Manifest & LLM text endpoints
RewriteRule ^robots\.txt$ robots.php [L,QSA]
RewriteRule ^sitemap\.xml$ sitemap.php [L,QSA]
RewriteRule ^manifest\.json$ manifest.php [L,QSA]
RewriteRule ^llms\.txt$ llms_auto.php [L,QSA]
RewriteRule ^llms[_-]full\.txt$ llms_auto.php?full=yes [L,QSA]

# Serve existing files and directories directly
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Front controller routing
RewriteRule ^(.*)$ index.php [L,QSA]

<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set Referrer-Policy "strict-origin-when-cross-origin"

    # 1 Year Cache for Static Images & Media
    <FilesMatch "\.(ico|jpe?g|png|gif|webp|avif|svg|mp4|webm|ogg)$">
        Header set Cache-Control "max-age=31536000, public, immutable"
    </FilesMatch>

    # 1 Year Cache for Web Fonts
    <FilesMatch "\.(woff2?|ttf|otf|eot)$">
        Header set Cache-Control "max-age=31536000, public, immutable"
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>

    # 30 Days Cache for Stylesheets & JavaScript
    <FilesMatch "\.(css|js)$">
        Header set Cache-Control "max-age=2592000, public"
    </FilesMatch>

    # 7 Days Cache for Web App Manifest
    <FilesMatch "\.(webmanifest|manifest\.json)$">
        Header set Cache-Control "max-age=604800, public"
    </FilesMatch>
</IfModule>
