# Dynamic Landing Page System - Apache Configuration

# Enable Rewrite Engine
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Force HTTPS (optional - uncomment if you have SSL)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Remove www (optional - uncomment if preferred)
    # RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    # RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Prevent access to sensitive files
    RewriteRule ^(.*\.php)$ - [F,L]
    RewriteCond %{REQUEST_URI} !(index\.php|enhanced_traffic_analyzer_fixed\.php)
    RewriteRule ^.*\.php$ - [F,L]

    # Prevent directory listing
    Options -Indexes

    # Protect log files
    <FilesMatch "traffic_log\.txt">
        Order Allow,Deny
        Deny from all
    </FilesMatch>

    # Protect cache directories
    RewriteRule ^content_cache/ - [F,L]

    # Handle 404 errors with index.php
    ErrorDocument 404 /index.php

    # Serve existing files directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # Allow access to analytics page
    RewriteCond %{REQUEST_URI} !^/enhanced_traffic_analyzer_fixed\.php
    
    # Route all other requests to index.php
    RewriteRule . /index.php [L]
</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"
    
    # Content Security Policy (adjust as needed)
    # Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://kit.fontawesome.com https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:;"
</IfModule>

# 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
    
    # 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/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType application/font-woff "access plus 1 year"
    ExpiresByType application/font-woff2 "access plus 1 year"
    
    # HTML
    ExpiresByType text/html "access plus 0 seconds"
</IfModule>

# UTF-8 Encoding
AddDefaultCharset UTF-8
<IfModule mod_mime.c>
    AddCharset UTF-8 .html .css .js .xml .json .txt
</IfModule>

# PHP Settings (if allowed)
<IfModule mod_php7.c>
    php_value upload_max_filesize 10M
    php_value post_max_size 10M
    php_value memory_limit 256M
    php_value max_execution_time 60
    php_value max_input_time 60
    
    # Disable display_errors in production
    # php_flag display_errors Off
    # php_value error_reporting 22527
</IfModule>

# Protect configuration files
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|sql|json)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# Allow access to pages.json specifically
<FilesMatch "^pages\.json$">
    Order Allow,Deny
    Allow from all
</FilesMatch>

# Prevent access to hidden files
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>
