# ActiveRevenue Self-Serve Ad Platform - SEO & Performance Optimization

# Enable Apache modules (if available)
<IfModule mod_rewrite.c>
RewriteEngine On

# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

# Handle external account redirects with proper status codes
RewriteRule ^login/?$ https://ui.activerevenue.com/pages/login.php [R=301,L]
RewriteRule ^signup/?$ https://ui.activerevenue.com/pages/login.php?signup [R=301,L]
RewriteRule ^register/?$ https://ui.activerevenue.com/pages/login.php?signup [R=301,L]
RewriteRule ^advertiser/?$ https://ui.activerevenue.com/pages/login.php?signup [R=301,L]
RewriteRule ^publisher/?$ https://dashboard.activerevenue.com/register [R=301,L]
RewriteRule ^dashboard/?$ https://ui.activerevenue.com/pages/login.php [R=301,L]

# Blog RSS and Sitemap proxy to Supabase Edge Functions
RewriteCond %{REQUEST_URI} ^/blog/rss\.xml$ [NC]
RewriteRule ^(.*)$ https://pdqorrgckydxynwhrrup.supabase.co/functions/v1/blog-rss [P,L]

RewriteCond %{REQUEST_URI} ^/blog/sitemap\.xml$ [NC]
RewriteRule ^(.*)$ https://pdqorrgckydxynwhrrup.supabase.co/functions/v1/blog-sitemap [P,L]

# Common ad format shortcuts
RewriteRule ^push/?$ /ad-formats/push [R=301,L]
RewriteRule ^popunder/?$ /ad-formats/popunder [R=301,L]
RewriteRule ^zero-click/?$ /ad-formats/zero-click [R=301,L]
RewriteRule ^floating-push/?$ /ad-formats/floating-push [R=301,L]
RewriteRule ^video/?$ /ad-formats/video [R=301,L]
RewriteRule ^telegram/?$ /ad-formats/telegram [R=301,L]

# Legacy page redirects
RewriteRule ^support/?$ /contact [R=301,L]
RewriteRule ^faq/?$ /help [R=301,L]
RewriteRule ^monetize/?$ /publishers [R=301,L]
RewriteRule ^terms/?$ /legal/terms-of-service [R=301,L]
RewriteRule ^privacy/?$ /legal/privacy-policy [R=301,L]
RewriteRule ^cookies/?$ /legal/cookie-policy [R=301,L]

# Handle React Router (History API fallback)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api/
RewriteRule . /index.html [L]

</IfModule>

# Security Headers
<IfModule mod_headers.c>
    # XSS Protection
    Header always set X-XSS-Protection "1; mode=block"
    
    # Prevent MIME sniffing
    Header always set X-Content-Type-Options "nosniff"
    
    # Clickjacking protection
    Header always set X-Frame-Options "SAMEORIGIN"
    
    # Referrer Policy
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # Content Security Policy
    Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' *.activerevenue.com *.googletagmanager.com *.google-analytics.com; style-src 'self' 'unsafe-inline' fonts.googleapis.com; font-src 'self' fonts.gstatic.com; img-src 'self' data: *.activerevenue.com *.google-analytics.com; connect-src 'self' *.activerevenue.com *.google-analytics.com;"
</IfModule>

# Compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE 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"
    
    # HTML and XML
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType text/xml "access plus 0 seconds"
    ExpiresByType application/xml "access plus 0 seconds"
    
    # Fonts
    ExpiresByType application/font-woff "access plus 1 year"
    ExpiresByType application/font-woff2 "access plus 1 year"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
    ExpiresByType application/x-font-ttf "access plus 1 year"
    
    # Default
    ExpiresDefault "access plus 1 month"
</IfModule>

# Error Pages
ErrorDocument 404 /index.html

# Disable server signature
ServerSignature Off

# Disable directory browsing
Options -Indexes

# Prevent access to sensitive files
<FilesMatch "\.(env|log|md|sql|sh|yml|yaml|lock|json)$">
    Require all denied
</FilesMatch>

# Robots.txt location
<Files "robots.txt">
    Header set Content-Type "text/plain"
</Files>