<IfModule mod_rewrite.c>
    RewriteEngine On
    # No RewriteBase: let mod_rewrite auto-detect it from this .htaccess file's own
    # directory, so the same file works whether this app sits at a domain root
    # (production, e.g. https://kiyp.org/magazine) or under a subfolder (this local
    # dev copy, http://localhost/kyp/magazine) without needing separate configs.

    # ── Step 1: externally redirect foo.php -> foo ──────────────────────────
    # Without this, both /page and /page.php work and serve identical content,
    # which Google treats as duplicate URLs. This sends a real 301 (permanent
    # redirect) so browsers, bookmarks, and search engines all settle on the
    # clean URL. Runs before the internal rewrite below, and only matches
    # requests that actually end in .php (so it won't loop).
    #
    # GET/HEAD only - deliberately excludes POST/PUT/DELETE/etc. Every AJAX
    # call in this app (admin.php's dispatcher, api/*.php, forgot-password,
    # login, ...) POSTs straight to a .php file. A 301 here would make
    # browsers replay those as bodyless GETs per fetch/XHR spec, silently
    # dropping the action/payload and breaking every POST endpoint sitewide
    # (this is what caused the admin dashboard to stop loading). Clean URLs
    # only matter for real page navigation, which is always GET/HEAD anyway.
    RewriteCond %{THE_REQUEST} ^(?:GET|HEAD)\s/+(.+)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=301,L]

    # ── Step 2: internally serve foo.php for a request to foo ───────────────
    # Don't touch requests for files/directories that already exist as-is
    # (static assets, uploads, existing .php files hit directly, etc.)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Only rewrite when a matching .php file actually exists
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ $1.php [L]
</IfModule>
