# ============================================================================
# LANDING PAGE SYSTEM - .htaccess Configuration
# ============================================================================

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

    # ========================================================================
    # SEO-FRIENDLY URL ROUTING
    # ========================================================================
    
    # Redirect www to non-www (or vice versa - uncomment one)
    # RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    # RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    
    # Force HTTPS (uncomment when SSL is set up)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # ========================================================================
    # KEYWORD-BASED ROUTING
    # ========================================================================
    
    # Route /keyword-here to index.php?keyword=keyword-here
    # Example: /weighted-blanket becomes ?keyword=weighted blanket
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(index\.php|dashboard\.php|install\.php|tracker\.php)
    RewriteCond %{REQUEST_URI} !^/(content_cache|assets|css|js|images)/
    RewriteRule ^([a-zA-Z0-9\-]+)/?$ index.php?keyword=$1 [L,QSA]
    
    # Route /category/keyword to index.php?keyword=keyword
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ index.php?keyword=$2 [L,QSA]
    
</IfModule>

# ============================================================================
# SECURITY SETTINGS
# ============================================================================

# Prevent directory browsing
Options -Indexes

# Protect sensitive files
<FilesMatch "(^#.*#|\.(bak|config|dist|fla|inc|ini|log|psd|sh|sql|sw[op])|~)$">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>

# Protect config files
<Files "config.php">
    Order allow,deny
    Deny from all
</Files>

# Protect log files
<Files "traffic_log.txt">
    Order allow,deny
    Deny from all
</Files>

# Block access to hidden files (.git, .htaccess, etc)
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

# ============================================================================
# PERFORMANCE OPTIMIZATION
# ============================================================================

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

# Browser Caching
<IfModule mod_expires.c>
    ExpiresActive On
    
    # Images
    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"
    ExpiresByType image/x-icon "access plus 1 year"
    
    # CSS and JavaScript
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    
    # Fonts
    ExpiresByType font/ttf "access plus 1 year"
    ExpiresByType font/otf "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    
    # HTML
    ExpiresByType text/html "access plus 0 seconds"
</IfModule>

# Cache-Control Headers
<IfModule mod_headers.c>
    # 1 YEAR - Fonts, Images
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|webp|svg|woff|woff2|ttf|otf)$">
        Header set Cache-Control "max-age=31536000, public"
    </FilesMatch>
    
    # 1 MONTH - CSS, JavaScript
    <FilesMatch "\.(css|js)$">
        Header set Cache-Control "max-age=2628000, public"
    </FilesMatch>
    
    # 0 SECONDS - HTML
    <FilesMatch "\.(html|htm|php)$">
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "0"
    </FilesMatch>
</IfModule>

# ============================================================================
# PHP SETTINGS
# ============================================================================

<IfModule mod_php7.c>
    # Error Reporting (set to 0 for production)
    php_flag display_errors Off
    php_flag display_startup_errors Off
    php_value error_reporting 0
    
    # Performance
    php_value max_execution_time 30
    php_value max_input_time 60
    php_value memory_limit 128M
    
    # File Uploads
    php_flag file_uploads On
    php_value upload_max_filesize 10M
    php_value post_max_size 10M
    
    # Security
    php_flag expose_php Off
    php_flag allow_url_fopen On
    php_flag allow_url_include Off
</IfModule>

# ============================================================================
# ERROR PAGES (Optional - create custom error pages)
# ============================================================================

# ErrorDocument 404 /404.html
# ErrorDocument 500 /500.html

# ============================================================================
# GZIP COMPRESSION (Alternative method)
# ============================================================================

<IfModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

# ============================================================================
# CHARACTER ENCODING
# ============================================================================

AddDefaultCharset UTF-8
AddCharset UTF-8 .html .css .js .xml .json .rss .atom

# ============================================================================
# MIME TYPES
# ============================================================================

<IfModule mod_mime.c>
    # JavaScript
    AddType application/javascript js jsonp
    AddType application/json json
    
    # Web fonts
    AddType font/woff woff
    AddType font/woff2 woff2
    AddType application/font-woff woff
    AddType application/x-font-woff woff
    AddType font/opentype otf
    AddType application/x-font-opentype otf
    AddType application/x-font-truetype ttf
    
    # SVG
    AddType image/svg+xml svg svgz
    AddEncoding gzip svgz
    
    # Other
    AddType application/octet-stream safariextz
    AddType application/x-chrome-extension crx
    AddType application/x-opera-extension oex
    AddType application/x-web-app-manifest+json webapp
    AddType text/cache-manifest appcache manifest
    AddType text/vtt vtt
    AddType text/x-component htc
</IfModule>

# ============================================================================
# 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 type sniffing
    Header always set X-Content-Type-Options "nosniff"
    
    # Referrer Policy
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # Content Security Policy (adjust as needed)
    # Header always set Content-Security-Policy "default-src 'self' https:; script-src 'self' 'unsafe-inline' https:; style-src 'self' 'unsafe-inline' https:; img-src 'self' https: data:; font-src 'self' https: data:;"
</IfModule>

# ============================================================================
# BOT PROTECTION (Optional)
# ============================================================================

# Block bad bots
<IfModule mod_rewrite.c>
    RewriteCond %{HTTP_USER_AGENT} (libwww-perl|wget|python|nikto|curl|scan|java|winhttp|clshttp|loader) [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} (;|<|>|'|%0A|%0D|%27|%3C|%3E|%00) [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} (bot|spider|crawler) [NC]
    # Uncomment to block:
    # RewriteRule ^.* - [F,L]
</IfModule>

# ============================================================================
# END OF CONFIGURATION
# ============================================================================
