# GistNaira — clean page addresses (/marketplace instead of /marketplace.php) and clean content
# addresses (/article/12345/headline-news, /poll/12349/…, /topic/12348/…).
# robots.txt and sitemap.xml follow the "Allow Search Engine Indexing" switch.
# (If mod_rewrite is unavailable, the static robots.txt — "Disallow: /" by default — is used.)

DirectoryIndex index.php index.html
# Folders get their trailing slash from the rules below instead, so /account (the page account.php)
# is not turned into /account/ (the folder) — that clash made /account load forever (redirect loop).
<IfModule mod_dir.c>
    DirectorySlash Off
</IfModule>
# Content negotiation would map /topic/12/x to topic.php on some hosts before the rules below.
Options -MultiViews

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^robots\.txt$ robots.php [L]
    RewriteRule ^sitemap\.xml$ sitemap.php [L]
    RewriteRule ^\.well-known/assetlinks\.json$ app-links.php [L]

    # 1) Content by numeric ID (the slug is only for people; topic.php 301s to the canonical address).
    RewriteRule ^(article|topic|poll)/([0-9]+)(?:/[^/]*)?/?$ topic.php?id=$2&_route=$1 [QSA,L]

    # Ads Manager: /ads-manager and /ads-manager/campaign/{id}
    RewriteRule ^ads-manager/?$ ads-manager/index.php [QSA,L]
    RewriteRule ^ads-manager/campaign/([0-9]+)/?$ ads-manager/campaign.php?id=$1 [QSA,L]

    # 2) Old links: /marketplace.php?x=1 → /marketplace?x=1 and /creator/index.php → /creator/ (301, GET/HEAD only,
    #    so form posts are never redirected). The Android API (api/), payment webhooks (webhooks/) and topic.php
    #    (which sends its own single 301 straight to /article|poll|topic/…) are left alone.
    RewriteCond %{REQUEST_METHOD} ^(GET|HEAD)$
    RewriteCond %{THE_REQUEST} !\s/+(?:[^?\s]*/)?(?:api|webhooks)/ [NC]
    RewriteCond %{THE_REQUEST} \s/+((?:[^?\s]*/)?)index\.php[?\s] [NC]
    RewriteRule ^ /%1 [R=301,L,NE]

    RewriteCond %{REQUEST_METHOD} ^(GET|HEAD)$
    RewriteCond %{THE_REQUEST} !\s/+(?:[^?\s]*/)?(?:api|webhooks)/ [NC]
    RewriteCond %{THE_REQUEST} !\s/+(?:[^?\s]*/)?topic\.php[?\s] [NC]
    RewriteCond %{THE_REQUEST} \s/+([^?\s]+)\.php[?\s] [NC]
    RewriteRule ^ /%1 [R=301,L,NE]

    # 3) Clean addresses: /marketplace → marketplace.php, /account/payout → account/payout.php
    #    (served internally — the address bar keeps the clean link; posts work the same).
    #    A page and a folder with the same name (account.php + account/, sponsored.php + sponsored/):
    #    /account is the PAGE, /account/ the folder — this used to loop between the two.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*[^/])$ $1.php [L]

    # 4) A real folder asked for without its slash (/creator → /creator/).
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^ %{REQUEST_URI}/ [R=301,L]
</IfModule>
