# Mona Mart — Next.js static export / Apache / cPanel
# Keep this file in the exported public root next to index.html.

DirectoryIndex index.html
Options -MultiViews

# SEO FIX 18 Nov 2026: unknown URLs used to return HTTP 200 (the SPA
# fallback below served index.html for EVERYTHING — a site-wide soft 404
# that let Google index the ৪০৪ page as a sitelink). Now a URL that
# matches no file, no directory and no known client-side route is a real
# HTTP 404, served with the 404 page below.
ErrorDocument 404 /404.html

<IfModule mod_setenvif.c>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Pass the Authorization header to PHP (cPanel FastCGI/CGI strips it by
    # default, which caused "Authentication required" on admin saves).
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # ==========================================================
    # CANONICAL DOMAIN
    # Force HTTPS + non-www so every entry point uses one URL.
    # ==========================================================
    RewriteCond %{HTTPS} !=on [OR]
    RewriteCond %{HTTP_HOST} ^www\.monamart\.com$ [NC]
    RewriteRule ^ https://demo1.techmission.store%{REQUEST_URI} [R=301,END,NE]

    # ==========================================================
    # BACKEND / UPLOADS MUST NEVER FALL THROUGH TO index.html
    # ==========================================================
    RewriteRule ^backend/api/ - [END]
    RewriteRule ^backend/uploads/ - [END]

    # ==========================================================
    # SEO: dynamic sitemap + per-product title/description/share image
    # ==========================================================
    RewriteRule ^sitemap\.xml$ /backend/api/sitemap.php [L]
    RewriteRule ^product/[^/]+/?$ /backend/api/product-page.php [L]

    # Home page + PWA manifest take the logo / SEO / preview image from
    # Admin > General Settings (served by PHP, falls back to the static file).
    RewriteRule ^(index\.html)?$ /backend/api/site-meta.php [L]
    RewriteRule ^manifest\.webmanifest$ /backend/api/manifest.php [L]

    # ==========================================================
    # EXISTING FILES AND DIRECTORIES
    # ==========================================================
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [END]

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [END]

    # ==========================================================
    # NEXT.JS STATIC EXPORT / SPA FALLBACK
    # Product/detail and other client-side routes are handled here.
    # ==========================================================
    # SEO FIX 18 Nov 2026 — the old catch-all `RewriteRule ^ index.html`
    # served the shell with HTTP 200 for ANY unknown URL (soft 404).
    # The fallback now applies ONLY to known client-side routes; anything
    # else falls through to a real HTTP 404 (ErrorDocument above).
    # Whitelist: every view the SPA shell can render (see the route
    # resolver in app/page.tsx), plus /page/ + /landing/ dynamic pages
    # and /admin/* + /payment/* deep links (same as before — they used to
    # fall through to the shell too, so their behaviour is unchanged).
    # Home (/) and /index.html are handled by the site-meta.php rule
    # above; /product/<slug> by the product-page.php rule above.
    RewriteCond %{REQUEST_URI} ^/(shop|cart|track|account|wishlist|orders|order-success|complaint|login|register|about|terms|privacy|contact|delivery|return-policy|how-to-order|help-center)/?$ [OR]
    RewriteCond %{REQUEST_URI} ^/(page|landing)/[^/]+/?$ [OR]
    RewriteCond %{REQUEST_URI} ^/(admin|payment)(/.*)?$
    RewriteRule ^ index.html [END]
</IfModule>

# ==============================================================
# CACHE SETTINGS
# ============================================================== 
<IfModule mod_expires.c>
    ExpiresActive On

    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    # Many servers label .js as text/javascript, which the line above misses.
    ExpiresByType text/javascript "access plus 1 year"
    ExpiresByType application/x-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/avif "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 month"

    ExpiresByType image/webp "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"

    ExpiresByType text/html "access plus 0 seconds"
</IfModule>

# Hashed Next.js build files never change, so cache them for a year.
<IfModule mod_headers.c>
    <FilesMatch "\.(js|css|woff2?)$">
        <If "%{REQUEST_URI} =~ m#^/_next/static/#">
            Header set Cache-Control "public, max-age=31536000, immutable"
        </If>
    </FilesMatch>
</IfModule>

# Service worker + push helper files must never be cached (otherwise phones keep an old sw.js).
<IfModule mod_headers.c>
    <FilesMatch "^(sw\.js|webpush-admin\.js)$">
        Header set Cache-Control "no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
    </FilesMatch>
</IfModule>

# Agentic browsing (ARD, added 2026-11-15): serve the agent manifest with its
# media type and let registries fetch it cross-origin.
<IfModule mod_headers.c>
    <FilesMatch "^(ard\.json|ai-catalog\.json)$">
        Header set Content-Type "application/ard+json"
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>
<IfModule mod_expires.c>
    <FilesMatch "^(sw\.js|webpush-admin\.js)$">
        ExpiresActive Off
    </FilesMatch>
</IfModule>

# ==============================================================
# SECURITY HEADERS
# ==============================================================
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "geolocation=(), camera=(), microphone=()"
    Header always set Strict-Transport-Security "max-age=31536000"
    # Content-Security-Policy — keep identical to next.config.ts (which only
    # applies when Next serves responses; this header is the live enforcement
    # for the static export on Apache). 'unsafe-inline' is kept for
    # script-src/style-src only because app/layout.tsx ships three static
    # inline scripts a static export cannot nonce; no inline script may come
    # from admin/user content.
    Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://www.googletagmanager.com https://connect.facebook.net https://analytics.tiktok.com https://embed.tawk.to https://accounts.google.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:; connect-src 'self' https://demo1.techmission.store https://www.googletagmanager.com https://connect.facebook.net https://analytics.tiktok.com https://api.cloudinary.com https://accounts.google.com; frame-src 'self' https://accounts.google.com https://www.google.com; object-src 'none'; base-uri 'self'; form-action 'self'"
</IfModule>

# ==============================================================
# SEO: X-ROBOTS-TAG (18 Nov 2026)
# Belt-and-braces noindex for the non-public pages, sent as an HTTP
# header so it applies even before any JavaScript runs. Every SPA view
# shares the same index.html shell, so the header is keyed on the
# REQUEST URI (what the visitor asked for), not on the file served.
# These pages ALSO carry a noindex meta tag (admin/payment layouts, or
# the shell's JS robots tag in app/page.tsx) — the header and the meta
# tag always agree. robots.txt deliberately does NOT disallow these
# paths: a disallowed URL's noindex is never read by Google, so the
# pages must stay crawlable for the noindex to take effect.
# ==============================================================
<IfModule mod_headers.c>
    # Admin + payment (login-gated, order ids in payment URLs).
    Header set X-Robots-Tag "noindex, nofollow" "expr=%{REQUEST_URI} =~ m#^/(admin|payment)(/|$)#"
    # Non-public SPA views: cart, order tracking, account areas,
    # login/register, order success, complaint.
    Header set X-Robots-Tag "noindex, nofollow" "expr=%{REQUEST_URI} =~ m#^/(cart|track|account|login|register|wishlist|orders|order-success|complaint)(/|$)#"
    # Shop search results (?search=...): canonicalised to /shop/ and
    # never indexed as their own URL.
    Header set X-Robots-Tag "noindex, nofollow" "expr=%{REQUEST_URI} =~ m#^/shop/?$# && %{QUERY_STRING} =~ /search=/"
    # The 404 page itself (direct /404 or /404.html hits).
    Header set X-Robots-Tag "noindex, nofollow" "expr=%{REQUEST_URI} =~ m#^/404#"
</IfModule>
# The 404 document served by ErrorDocument for genuinely unknown URLs:
# the header above keys on the requested URI (e.g. /old-link), so this
# FilesMatch covers the 404.html file itself, whichever URL triggered it.
<IfModule mod_headers.c>
    <FilesMatch "404\.html">
        Header set X-Robots-Tag "noindex, nofollow"
    </FilesMatch>
</IfModule>

<IfModule mod_mime.c>
    AddType application/manifest+json .webmanifest
</IfModule>

# ==============================================================
# GZIP / DEFLATE
# ============================================================== 
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# ==============================================================
# cPanel PHP HANDLER
# Keep the handler so the backend PHP remains executable when the
# exported frontend is copied into the same public_html root.
# ============================================================== 
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php84” package as the default “PHP” programming language.
<IfModule mime_module>
    AddHandler application/x-httpd-ea-php84 .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler
