# ============================================================================
#  Document Publishing Platform - root .htaccess
#  Clean, extensionless URLs for cPanel / standard Apache hosting (mod_rewrite)
#  Works in the domain root AND in a sub-folder (e.g. example.com/library/)
# ============================================================================

# Never list directory contents, never guess file names
Options -Indexes -MultiViews
DirectoryIndex index.php

# Stop "/index.php/anything" style PATH_INFO URLs from resolving
AcceptPathInfo Off

# Never serve dot-files (.env, .git, .user.ini ...)
<FilesMatch "^\.">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order deny,allow
        Deny from all
    </IfModule>
</FilesMatch>

# Upload limits (only applied when PHP runs as an Apache module; PHP-FPM/CGI
# hosts use .user.ini instead - see that file).
<IfModule mod_php.c>
    php_value upload_max_filesize 64M
    php_value post_max_size 70M
    php_value max_file_uploads 50
    php_flag display_errors off
</IfModule>
<IfModule mod_php8.c>
    php_value upload_max_filesize 64M
    php_value post_max_size 70M
    php_value max_file_uploads 50
    php_flag display_errors off
</IfModule>
<IfModule mod_php7.c>
    php_value upload_max_filesize 64M
    php_value post_max_size 70M
    php_value max_file_uploads 50
    php_flag display_errors off
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # ------------------------------------------------------------------
    # 1. Internal folders and project files are never reachable over HTTP
    # ------------------------------------------------------------------
    RewriteRule ^(app|storage)(/|$) - [F,L]
    RewriteRule ^(database\.sql|README\.md|INSTALL\.md)$ - [F,L]

    # ------------------------------------------------------------------
    # 2. Hide ".php": redirect any direct request for a .php URL to its
    #    clean equivalent. THE_REQUEST is the browser's original request
    #    line, so internal rewrites to index.php can never trigger a loop.
    # ------------------------------------------------------------------
    # /index.php  ->  /      (also works inside a sub-folder)
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s(/[^?\s]*?)index\.php(?:\?\S*)?\sHTTP/ [NC]
    RewriteRule ^ %1 [R=301,L]

    # /anything.php  ->  /anything
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s(/[^?\s]*?)\.php(?:\?\S*)?\sHTTP/ [NC]
    RewriteRule ^ %1 [R=301,L]

    # ------------------------------------------------------------------
    # 3. Real files and folders (CSS, JS, images, uploaded images) are
    #    served directly and never touched by the router
    # ------------------------------------------------------------------
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # ------------------------------------------------------------------
    # 4. Everything else goes to the front controller, which routes it
    #    (documents, categories, downloads, /admin/*, AJAX endpoints ...)
    #    The relative target keeps this working in sub-folder installs.
    # ------------------------------------------------------------------
    RewriteRule ^ index.php [L]
</IfModule>

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

# Browser caching for static assets (cache-busted with ?v=filemtime in templates)
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 30 days"
    ExpiresByType application/javascript "access plus 30 days"
    ExpiresByType text/javascript "access plus 30 days"
    ExpiresByType image/png "access plus 30 days"
    ExpiresByType image/jpeg "access plus 30 days"
    ExpiresByType image/webp "access plus 30 days"
    ExpiresByType image/gif "access plus 30 days"
    ExpiresByType image/x-icon "access plus 30 days"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
</IfModule>
