# ─────────────────────────────────────────────────────────────────────────────
#  AigentZone — static Next.js export on Apache / cPanel public_html
#
#  This file is in Next's public/, so `next build` copies it to out/.htaccess
#  and it deploys with the site. It replaces what src/middleware.ts used to do:
#  the legacy 301s, and serving extensionless URLs from the exported .html.
# ─────────────────────────────────────────────────────────────────────────────

# MultiViews MUST stay off. With it on, Apache content-negotiates between
# about.html and about.txt — and about.txt is the RSC payload the client router
# fetches during navigation, so a negotiated match breaks in-page navigation.
Options -Indexes -MultiViews

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# ── 1. Canonical host and scheme ────────────────────────────────────────────
# First, so a legacy URL on http://www redirects once rather than twice.
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\.aigentzone\.ae$ [NC]
RewriteRule ^ https://aigentzone.ae%{REQUEST_URI} [R=301,L]

# ── 2. Legacy product.html?platform=… → clean 301 ───────────────────────────
# QSD drops the incoming query string, so the crawler lands on exactly the
# canonical URL. This is the behaviour src/middleware.ts existed to provide —
# next.config redirects cannot do it, they re-append the query.
RewriteCond %{QUERY_STRING} (^|&)platform=omni-channel(&|$)
RewriteRule ^product\.html$ /omnilink [R=301,L,QSD]

RewriteCond %{QUERY_STRING} (^|&)platform=aigentpos(&|$)
RewriteRule ^product\.html$ /aigentpos [R=301,L,QSD]

RewriteCond %{QUERY_STRING} (^|&)platform=business-telephony(&|$)
RewriteRule ^product\.html$ /solutions/cloud-pbx [R=301,L,QSD]

RewriteCond %{QUERY_STRING} (^|&)platform=ai-agents(&|$)
RewriteRule ^product\.html$ /solutions/ai-solutions [R=301,L,QSD]

RewriteCond %{QUERY_STRING} (^|&)platform=ai-analytics(&|$)
RewriteRule ^product\.html$ /solutions/ai-solutions [R=301,L,QSD]

# product.html with a missing or unrecognised platform. The middleware let this
# fall through to a 404; sending it to the solutions hub keeps the link value.
RewriteRule ^product\.html$ /solutions [R=301,L,QSD]

# ── 3. Legacy static pages ──────────────────────────────────────────────────
RewriteRule ^index\.html$          /                [R=301,L,QSD]
RewriteRule ^privacy-policy\.html$ /privacy-policy  [R=301,L,QSD]
RewriteRule ^data-deletion\.html$  /data-deletion   [R=301,L,QSD]

# ── 4. One canonical form per URL: no trailing slash ────────────────────────
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

# ── 5. Extensionless URL → the exported .html ───────────────────────────────
# This has to run BEFORE the "real file or directory" pass below: /ar is both a
# file (ar.html, the Arabic homepage) and a directory (ar/, its subpages), and
# the file has to win.
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]

# ── 6. Anything that exists on disk is served as-is ─────────────────────────
# /_next/**, /assets/**, /contact-handler.php, sitemap.xml, robots.txt,
# llms.txt, and the *.txt RSC payloads the client router fetches.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
</IfModule>

ErrorDocument 404 /404.html

# ── Caching ─────────────────────────────────────────────────────────────────
<IfModule mod_headers.c>
  # Everything under /_next/static is content-hashed and safe to keep forever.
  SetEnvIf Request_URI "^/_next/static/" NEXT_IMMUTABLE=1
  Header set Cache-Control "public, max-age=31536000, immutable" env=NEXT_IMMUTABLE

  # HTML and RSC payloads must revalidate so a redeploy is picked up at once.
  <FilesMatch "\.(html|txt)$">
    Header set Cache-Control "public, max-age=0, must-revalidate"
  </FilesMatch>

  # The contact endpoint must never be cached by a proxy.
  <FilesMatch "^contact-handler\.php$">
    Header set Cache-Control "no-store"
  </FilesMatch>
</IfModule>

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml \
                                application/javascript application/json \
                                image/svg+xml
</IfModule>

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/webp    "access plus 6 months"
  ExpiresByType image/png     "access plus 6 months"
  ExpiresByType image/svg+xml "access plus 6 months"
  ExpiresByType font/woff2    "access plus 1 year"
</IfModule>

# ── Hardening ───────────────────────────────────────────────────────────────
<IfModule mod_headers.c>
  Header set X-Content-Type-Options "nosniff"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# The contact endpoint reads its SMTP password from a file outside public_html.
# Should a copy ever be placed here by mistake, refuse to serve it.
<FilesMatch "aigentzone-contact-config\.php$">
  Require all denied
</FilesMatch>
