<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Force HTTPS (Uncomment if SSL is installed)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

    # ---- Hide .php in the address bar, for customer-facing pages only ----
    # A NARROW WHITELIST, on purpose: an earlier, broader version of this ("redirect any
    # .php request to its clean URL") caused a rewrite/permission loop on this host —
    # visible as a raw Apache 403 page — specifically on admin GET actions like deleting a
    # category. Scoping this to just the pages below, and leaving /admin/ (and every file
    # that must keep its .php extension — google-callback.php is registered as-is with
    # Google's OAuth console; sw.php and manifest.php are fetched by exact URL for the PWA;
    # quick-action.php, run-migration.php, setup-google-login.php, _healthcheck.php,
    # 404.php) completely untouched avoids that failure mode entirely.
    RewriteCond %{THE_REQUEST} \s/+(about|login|register|logout|cart|checkout|category|product|search|wishlist|profile|personal-info|change-password|forgot-password|reset-password|order-history|order-confirmation|cancel-order|submit-payment)\.php(\?[^\s]*)?\s [NC]
    RewriteRule ^ /%1%2 [R=301,L]

    # index.php (and bare /index) -> / — the homepage is always naklamart.com, never
    # .../index.php or .../index in the address bar.
    RewriteCond %{THE_REQUEST} \s/+index(?:\.php)?(\?[^\s]*)?\s [NC]
    RewriteRule ^ /%1 [R=301,L]

    # ---- Clean URLs: /page also works without .php ----
    # If a clean URL (no .php) is requested and a matching .php file exists, serve it
    # internally — this is what makes the redirect above land somewhere real, and also
    # lets any link already written without ".php" (see includes/topbar.php etc.) work.
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [L]

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

    # Block directory browsing
    Options -Indexes

    # Block access to sensitive files
    <FilesMatch "\.(sql|log|ini|htaccess|htpasswd|json|md)$">
        Order allow,deny
        Deny from all
    </FilesMatch>

    # Security hardening: includes/ contains config.php, db.php, functions.php, auth.php —
    # these are only ever meant to be require()'d by other scripts, never requested directly
    # in a browser. Blocking direct access removes any chance of them being hit in an
    # unexpected way (e.g. a server misconfiguration that serves .php as plain text).
    RewriteRule ^includes/ - [F,L]

    # Security hardening: never execute PHP inside uploads/ — product images, user photos,
    # and banners are the only things that belong there. If an attacker ever managed to get
    # a disguised .php file into an upload folder, this stops it from running as code.
    RewriteRule ^uploads/.*\.php$ - [F,L]

    # Error pages
    ErrorDocument 404 /404.php
    ErrorDocument 403 /404.php
</IfModule>

# Security response headers — sent on every request. Wrapped in IfModule so nothing
# breaks on a host where mod_headers isn't enabled (headers are simply skipped, the
# rest of the site keeps working normally).
<IfModule mod_headers.c>
    # Stops the browser from guessing/"sniffing" a different content-type than what the
    # server declared (e.g. treating an uploaded image as HTML/JS and executing it).
    Header always set X-Content-Type-Options "nosniff"
    # Stops the entire site from being embedded in an <iframe> on another domain —
    # blocks clickjacking attacks (tricking a logged-in admin/customer into clicking
    # something on this site while it's invisibly framed under attacker content).
    Header always set X-Frame-Options "SAMEORIGIN"
    # Limits how much of this site's URL is leaked to other sites when someone clicks
    # an outbound link (e.g. a product page's full query string isn't sent as Referer
    # to an external site — only the origin is, and only cross-site).
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    # This site never needs the camera, microphone, or device geolocation — explicitly
    # denying them means even a compromised/malicious third-party script embedded later
    # (e.g. via a CDN) can't invoke a permission prompt for them.
    Header always set Permissions-Policy "geolocation=(), camera=(), microphone=()"
</IfModule>

# Belt-and-suspenders version of the uploads/ PHP-execution block above, in case
# mod_rewrite isn't active on this host — this uses a different Apache module.
<IfModule mod_php7.c>
    <Directory "uploads">
        php_flag engine off
    </Directory>
</IfModule>
<IfModule mod_php.c>
    <Directory "uploads">
        php_flag engine off
    </Directory>
</IfModule>
