# =============================================================================
# CASHLEX — Apache Configuration
# Domain: https://eliteqash.com (root install)
# =============================================================================

Options -Indexes -MultiViews
ServerSignature Off

# HTTPS Enforcement
<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# Security Headers
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options     "nosniff"
    Header always set X-Frame-Options            "DENY"
    Header always set X-XSS-Protection           "1; mode=block"
    Header always set Referrer-Policy            "strict-origin-when-cross-origin"
    Header always set Permissions-Policy         "geolocation=(), microphone=(), camera=()"
    Header always unset X-Powered-By
    Header always unset Server
</IfModule>

# Block sensitive files
<FilesMatch "^\.">
    Require all denied
</FilesMatch>

<FilesMatch "\.(env|sql|md|log|sh|gitignore|lock)$">
    Require all denied
</FilesMatch>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Block direct access to sensitive directories
    RewriteRule ^(config|includes|database|cron|storage)(/|$) - [F,L]

    # Let real files and directories pass through
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # Installable app (PWA): manifest, service worker (root scope) and icons
    RewriteRule ^manifest\.webmanifest$        pwa/manifest.php                   [L,QSA]
    RewriteRule ^sw\.js$                       pwa/sw.php                         [L,QSA]
    RewriteRule ^pwa-icon-([0-9]+)\.png$       pwa/icon.php?s=$1                  [L,QSA]
    RewriteRule ^og-image\.png$                pwa/og.php                          [L,QSA]
    RewriteRule ^favicon\.ico$                 pwa/icon.php?s=48                   [L,QSA]

    # =========================================================================
    # API ROUTES
    # =========================================================================
    RewriteRule ^api/payment/initiate$          api/payment/lipalink.php    [L,QSA]
    RewriteRule ^api/payment/status$            api/payment/webhook.php     [L,QSA]
    RewriteRule ^api/payment/check-fee$     api/payment/check-fee.php      [L,QSA]
    RewriteRule ^api/payment/webhook$           api/payment/webhook.php     [L,QSA]
    RewriteRule ^api/payment/gw/([a-z_]+)$      api/payment/gw.php?gw=$1    [L,QSA]
    RewriteRule ^pay/return/([a-z_]+)$          pay/return.php?gw=$1        [L,QSA]
    RewriteRule ^pay/go$                        pay/go.php                  [L,QSA]
    RewriteRule ^account/currency$              account/currency.php        [L,QSA]
    RewriteRule ^api/chat/send$                 api/chat/send.php           [L,QSA]
    RewriteRule ^api/chat/history$              api/chat/history.php        [L,QSA]
    RewriteRule ^api/chat/claim$                api/chat/claim.php          [L,QSA]
    RewriteRule ^api/manual-unlock$             api/manual-unlock.php       [L,QSA]
    RewriteRule ^api/admin/test-llm$            api/admin/test-llm.php      [L,QSA]
    RewriteRule ^api/admin/list-llm-models$     api/admin/list-llm-models.php [L,QSA]
    RewriteRule ^api/tasks/hotel-reviews$       api/tasks/hotel-reviews.php [L,QSA]
    RewriteRule ^api/tasks/app-reviews$         api/tasks/app-reviews.php [L,QSA]
    RewriteRule ^api/tasks/ai-training$         api/tasks/ai-training.php [L,QSA]
    RewriteRule ^api/admin/app-fetch$           api/admin/app-fetch.php [L,QSA]
    RewriteRule ^api/admin/app-import$          api/admin/app-import.php [L,QSA]
    RewriteRule ^api/product-download$          api/product-download.php    [L,QSA]
    RewriteRule ^api/branding-logo$             api/branding-logo.php       [L,QSA]
    RewriteRule ^api/media$                     api/media.php               [L,QSA]
    RewriteRule ^api/check-availability$      api/check-availability.php         [L,QSA]
    RewriteRule ^api/spin$                      api/spin.php                [L,QSA]
    RewriteRule ^api/tasks/([a-z-]+)$           api/tasks/$1.php            [L,QSA]
    RewriteRule ^api/payment/([a-z-]+)$         api/payment/$1.php          [L,QSA]
    RewriteRule ^api/([a-z-]+)$                 api/$1.php                  [L,QSA]

    # =========================================================================
    # AUTH ROUTES
    # =========================================================================
    RewriteRule ^auth/register$                 auth/register.php           [L,QSA]
    RewriteRule ^contact/?$                     pages/contact.php           [L,QSA]
    RewriteRule ^terms/?$                       pages/legal.php?doc=terms   [L,QSA]
    RewriteRule ^privacy/?$                     pages/legal.php?doc=privacy [L,QSA]
    RewriteRule ^auth/login$                    auth/login.php              [L,QSA]
    RewriteRule ^auth/logout$                   auth/logout.php             [L,QSA]
    RewriteRule ^auth/activate$                 auth/activate.php           [L,QSA]
    RewriteRule ^loans/apply$                   loans/apply.php             [L,QSA]
    RewriteRule ^loans/history$                 loans/history.php           [L,QSA]
    RewriteRule ^auth/forgot-password$          auth/forgot-password.php    [L,QSA]

    # =========================================================================
    # DASHBOARD
    # =========================================================================
    RewriteRule ^dashboard/?$                   dashboard/index.php         [L,QSA]

    # =========================================================================
    # TEAM
    # =========================================================================
    RewriteRule ^team/level-1-active$           team/level-1-active.php     [L,QSA]
    RewriteRule ^team/level-1-inactive$         team/level-1-inactive.php   [L,QSA]
    RewriteRule ^team/level-2-active$           team/level-2-active.php     [L,QSA]
    RewriteRule ^team/level-2-inactive$         team/level-2-inactive.php   [L,QSA]
    RewriteRule ^team/level-3-active$           team/level-3-active.php     [L,QSA]
    RewriteRule ^team/level-3-inactive$         team/level-3-inactive.php   [L,QSA]

    # =========================================================================
    # TOURNAMENT
    # =========================================================================
    RewriteRule ^tournament/?$                  tournament/index.php        [L,QSA]

    # =========================================================================
    # FINANCIAL
    # =========================================================================
    RewriteRule ^financial/deposit$             financial/deposit.php            [L,QSA]
    RewriteRule ^financial/deposit-history$     financial/deposit-history.php    [L,QSA]
    RewriteRule ^financial/reversal-history$    financial/reversal-history.php   [L,QSA]
    RewriteRule ^financial/bonuses$             financial/bonuses.php            [L,QSA]
    RewriteRule ^financial/commissions$         financial/commissions.php        [L,QSA]
    RewriteRule ^financial/withdraw$            financial/withdraw.php           [L,QSA]
    RewriteRule ^financial/withdrawal-history$  financial/withdrawal-history.php [L,QSA]
    RewriteRule ^financial/transactions$        financial/transactions.php       [L,QSA]

    # =========================================================================
    # SITE TASKS — Hotel Reviews
    # =========================================================================
    RewriteRule ^tasks/hotel-reviews/withdraw$  site-tasks/hotel-reviews/withdraw.php [L,QSA]
    RewriteRule ^tasks/hotel-reviews/?$         site-tasks/hotel-reviews/index.php    [L,QSA]
    RewriteRule ^tasks/app-reviews/?$           site-tasks/app-reviews/index.php      [L,QSA]
    RewriteRule ^tasks/youtube-videos/?$        site-tasks/video-tasks.php?platform=youtube  [L,QSA]
    RewriteRule ^tasks/tiktok-videos/?$         site-tasks/video-tasks.php?platform=tiktok   [L,QSA]

    # =========================================================================
    # SITE TASKS — Chatting
    # =========================================================================
    RewriteRule ^tasks/chatting/teach-swahili$  site-tasks/chatting/teach-swahili.php                    [L,QSA]
    RewriteRule ^tasks/chatting/asians$         site-tasks/chatting/chat-foreigners/chat-asians.php      [L,QSA]
    RewriteRule ^tasks/chatting/europeans$      site-tasks/chatting/chat-foreigners/chat-europeans.php   [L,QSA]
    RewriteRule ^tasks/chatting/americans$      site-tasks/chatting/chat-foreigners/chat-americans.php   [L,QSA]

    # =========================================================================
    # SITE TASKS — Others
    # =========================================================================
    # N5.9.0 Short Drama (Time Pass)
    RewriteRule ^tasks/short-drama/trailers/?$            site-tasks/short-drama/trailers.php         [L,QSA]
    RewriteRule ^tasks/short-drama/trailers/([0-9]+)/?$   site-tasks/short-drama/trailers.php?id=$1   [L,QSA]
    RewriteRule ^tasks/short-drama/?$                     site-tasks/short-drama/index.php            [L,QSA]
    RewriteRule ^tasks/short-drama/series/([0-9]+)/?$     site-tasks/short-drama/index.php?series=$1  [L,QSA]
    RewriteRule ^tasks/short-drama/watch/([0-9]+)/?$      site-tasks/short-drama/index.php?ep=$1      [L,QSA]
    RewriteRule ^tasks/short-drama/stream/([0-9]+)/?$     site-tasks/short-drama/stream.php?ep=$1     [L,QSA]
    RewriteRule ^api/tasks/short-drama$                   api/tasks/short-drama.php                   [L,QSA]
    RewriteRule ^tasks/ai-training/?$           site-tasks/ai-training/index.php   [L,QSA]
    RewriteRule ^tasks/ai-training/([a-z]+)/?$  site-tasks/ai-training/index.php?t=$1 [L,QSA]
    RewriteRule ^tasks/map-reviews/?$           site-tasks/map-reviews/index.php   [L,QSA]
    RewriteRule ^tasks/app-testing/?$           site-tasks/app-testing/index.php   [L,QSA]
    RewriteRule ^tasks/web-testing/?$           site-tasks/web-testing/index.php   [L,QSA]
    RewriteRule ^tasks/walk-earn/?$             site-tasks/walk-earn.php           [L,QSA]
    RewriteRule ^tasks/surveys/?$               site-tasks/site-surveys/index.php  [L,QSA]
    RewriteRule ^tasks/bet-spin$                site-tasks/bet-spin.php            [L,QSA]
    RewriteRule ^tasks/site-products$           site-tasks/site-products.php       [L,QSA]
    RewriteRule ^tasks/premium-products$        site-tasks/premium-products.php    [L,QSA]
    RewriteRule ^tasks/forex-trading$           site-tasks/forex-trading.php       [L,QSA]
    RewriteRule ^tasks/y99$                     site-tasks/y99.php                 [L,QSA]
    RewriteRule ^tasks/feature/([a-z0-9-]+)$    site-tasks/feature.php?f=$1        [L,QSA]

    # =========================================================================
    # ACCOUNT
    # =========================================================================
    RewriteRule ^account/profile$               account/profile.php         [L,QSA]
    RewriteRule ^account/pay-for-client$        account/pay-for-client.php  [L,QSA]
    RewriteRule ^account/accounts$              account/accounts.php        [L,QSA]

    # =========================================================================
    # ADMIN
    # =========================================================================
    RewriteRule ^admin/?$                       admin/index.php                    [L,QSA]
    RewriteRule ^admin/login$                   admin/login.php                    [L,QSA]
    RewriteRule ^admin/users$                   admin/users.php                    [L,QSA]
    RewriteRule ^admin/admins$                  admin/admins.php                   [L,QSA]
    RewriteRule ^admin/earnings$                admin/earnings.php                 [L,QSA]
    RewriteRule ^admin/user-edit$               admin/user-edit.php                [L,QSA]
    RewriteRule ^admin/transactions$            admin/transactions.php             [L,QSA]
    RewriteRule ^admin/payment-log$            admin/payment-log.php       [L,QSA]
    RewriteRule ^admin/revoke$                  admin/revoke.php                   [L,QSA]
    RewriteRule ^admin/withdrawals$             admin/withdrawals.php              [L,QSA]
    RewriteRule ^admin/auto-withdrawals$        admin/auto-withdrawals.php         [L,QSA]
    RewriteRule ^admin/bonuses$                 admin/bonuses.php                  [L,QSA]
    RewriteRule ^admin/access-control$          admin/access-control.php           [L,QSA]
    RewriteRule ^admin/announcements$           admin/announcements.php            [L,QSA]
    RewriteRule ^admin/countries$               admin/countries.php                [L,QSA]
    RewriteRule ^admin/activation-instructions$ admin/activation-instructions.php  [L,QSA]
    RewriteRule ^admin/payment-settings$        admin/payment-settings.php         [L,QSA]
    RewriteRule ^admin/payment-accounts$        admin/payment-accounts.php         [L,QSA]
    RewriteRule ^admin/manual-payments$         admin/manual-payments.php          [L,QSA]
    RewriteRule ^admin/theme-settings$          admin/theme-settings.php           [L,QSA]
    RewriteRule ^admin/app-settings$            admin/app-settings.php             [L,QSA]
    RewriteRule ^admin/email-settings$          admin/email-settings.php           [L,QSA]
    RewriteRule ^admin/wallets$                 admin/wallets.php                  [L,QSA]
    RewriteRule ^admin/analytics$               admin/analytics.php                [L,QSA]
    RewriteRule ^admin/activity$                admin/activity.php                 [L,QSA]
    RewriteRule ^admin/loans$                   admin/loans.php                    [L,QSA]
    RewriteRule ^admin/commissions$             admin/commissions.php              [L,QSA]
    RewriteRule ^admin/commission-audit$        admin/commission-audit.php         [L,QSA]
    RewriteRule ^admin/commission-rates$        admin/commission-rates.php         [L,QSA]
    RewriteRule ^admin/tasks/hotel-reviews$     admin/tasks/hotel-reviews.php      [L,QSA]
    RewriteRule ^admin/tasks/app-reviews$       admin/tasks/app-reviews.php        [L,QSA]
    RewriteRule ^admin/tasks/ai-training$       admin/tasks/ai-training.php        [L,QSA]
    RewriteRule ^admin/tasks/short-drama$       admin/tasks/short-drama.php        [L,QSA]
    RewriteRule ^admin/tasks/drama-trailers$    admin/tasks/drama-trailers.php     [L,QSA]
    RewriteRule ^admin/tasks/video-tasks$      admin/tasks/video-tasks.php        [L,QSA]
    RewriteRule ^admin/chat-profiles$           admin/chat-profiles.php            [L,QSA]
    RewriteRule ^admin/tasks/chat-tasks$       admin/tasks/chat-tasks.php         [L,QSA]
    RewriteRule ^admin/tasks/surveys$           admin/tasks/surveys.php            [L,QSA]
    RewriteRule ^admin/tasks/products$          admin/tasks/products.php           [L,QSA]
    RewriteRule ^admin/tasks/task-settings$     admin/tasks/task-settings.php      [L,QSA]
    RewriteRule ^admin/tasks/features$          admin/tasks/features.php           [L,QSA]
    RewriteRule ^admin/display-names$           admin/display-names.php            [L,QSA]
    RewriteRule ^admin/bulk-pricing$            admin/bulk-pricing.php             [L,QSA]
    RewriteRule ^admin/tasks/visibility$        admin/tasks/visibility.php         [L,QSA]

    # =========================================================================
    # ROOT
    # =========================================================================
    RewriteRule ^$                              index.php                   [L,QSA]

    # Safety net: any other address without ".php" (e.g. a new admin page)
    # opens its .php file when one exists.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([A-Za-z0-9_/-]+?)/?$          $1.php                      [L,QSA]

</IfModule>

# Cache static assets — CSS/JS are versioned (?v=ASSET_VER), so browsers may keep them for a year.
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css                 "access plus 1 year"
    ExpiresByType application/javascript   "access plus 1 year"
    ExpiresByType text/javascript          "access plus 1 year"
    ExpiresByType font/woff2               "access plus 1 year"
    ExpiresByType font/woff                "access plus 1 year"
    ExpiresByType application/font-woff2   "access plus 1 year"
    ExpiresByType image/jpeg               "access plus 3 months"
    ExpiresByType image/png                "access plus 3 months"
    ExpiresByType image/webp               "access plus 3 months"
    ExpiresByType image/avif               "access plus 3 months"
    ExpiresByType image/gif                "access plus 3 months"
    ExpiresByType image/svg+xml            "access plus 1 month"
    ExpiresByType image/x-icon             "access plus 1 month"
    ExpiresByType application/manifest+json "access plus 1 day"
    ExpiresByType text/html                "access plus 0 seconds"
</IfModule>
<IfModule mod_headers.c>
    <FilesMatch "\.(css|js|woff2?|ttf|otf)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
    <FilesMatch "\.(webp|avif|png|jpe?g|gif|svg|ico)$">
        Header set Cache-Control "public, max-age=7776000"
    </FilesMatch>
    # Pages are personal: never cached by shared proxies
    <FilesMatch "\.php$">
        Header set Cache-Control "private, no-cache"
    </FilesMatch>
    Header unset ETag
</IfModule>
FileETag None
<IfModule mod_mime.c>
    AddType font/woff2 .woff2
    AddType image/avif .avif
    AddType application/manifest+json .webmanifest
</IfModule>

# Compression (Brotli when the server has it, otherwise gzip)
<IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI_COMPRESS text/html text/css text/plain text/xml application/javascript text/javascript application/json image/svg+xml application/xml application/manifest+json
</IfModule>
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/javascript text/javascript application/json image/svg+xml application/xml application/manifest+json font/ttf font/otf
</IfModule>
