# Tiveli - Apache Configuration
# Security and routing configuration

# Prevent directory listing
Options -Indexes

# Enable error documents
ErrorDocument 404 /Tiveli/index.php
ErrorDocument 403 /Tiveli/index.php

# Protect sensitive files
<FilesMatch "^(database\.sql|README\.md|composer\.(json|lock)|\.env|\.git)">
    Require all denied
</FilesMatch>

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

# Enable mod_rewrite (optional, for clean URLs)
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /Tiveli/

    # Tiveli Business has been removed from this deployment.
    RewriteRule ^(business|api/business)(/|$) - [G,L,NC]
    RewriteRule ^templates(/|$) - [G,L,NC]

    # Protect private application directories and uploaded scripts
    RewriteRule ^(config|storage|includes)(/|$) - [F,L,NC]
    RewriteRule ^@([a-zA-Z0-9_]{3,50})/showcase/?$ users/showcase.php?username=$1 [END,QSA,NC]
    RewriteRule ^sites/([a-z0-9-]+)/?$ sites/view.php?slug=$1 [END,NC,QSA]
    RewriteRule ^sites/report\.php$ sites/report.php [END,NC]
    RewriteRule ^uploads/(status|messages|moments)(/|$) - [F,L,NC]
    RewriteRule ^uploads/.*\.(php[0-9]?|phtml|phar)$ - [F,L,NC]
    
    # Force HTTPS (uncomment for production)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # Remove trailing slashes
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
</IfModule>

# Cache static assets
<IfModule mod_expires.c>
    ExpiresActive On
    
    # Images
    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"
    
    # CSS and JavaScript
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    
    # Fonts
    ExpiresByType font/ttf "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    
    # Default
    ExpiresDefault "access plus 1 month"
</IfModule>

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

# PHP settings (if allowed)
<IfModule mod_php7.c>
    php_value upload_max_filesize 50M
    php_value post_max_size 51M
    php_value max_execution_time 300
    php_value max_input_time 300
    php_flag display_errors Off
    php_flag log_errors On
</IfModule>
