# Apache / cPanel configuration — for Namecheap shared hosting and anything else
# running Apache. Ignored by Netlify and Cloudflare Pages, which read the
# `_redirects` and `_headers` files beside it instead.
#
# This file has to live in public_html alongside index.html. It is a dotfile, so
# cPanel's File Manager hides it until you tick "Show hidden files" in Settings.

# ------------------------------------------------------------------ MIME types
# Apache does not know .glb, and a 3D model served as text/plain will not load.
<IfModule mod_mime.c>
  AddType model/gltf-binary         .glb
  AddType model/gltf+json           .gltf
  AddType image/svg+xml             .svg
  AddType application/manifest+json .webmanifest
  AddType font/woff2                .woff2
</IfModule>

# ----------------------------------------------------------------- compression
# Text compresses enormously — the main JavaScript bundle goes from about 520 kB
# to 134 kB. The .glb models are already compressed binary and are left alone.
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
  AddOutputFilterByType DEFLATE application/javascript application/json
  AddOutputFilterByType DEFLATE application/xml image/svg+xml
</IfModule>

# --------------------------------------------------------------------- caching
# Split by whether a filename changes when its contents do. The build
# fingerprints JavaScript and CSS, so those can be cached forever. index.html
# keeps its name and names all the others, so caching it is how visitors get
# stranded on an old deploy, loading chunks that no longer exist.
<IfModule mod_headers.c>
  <FilesMatch "\.(js|css)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>

  <FilesMatch "^index\.html$">
    Header set Cache-Control "public, max-age=0, must-revalidate"
  </FilesMatch>

  # Assets keep their own names, so a year would leave anyone who has seen a
  # design stuck with the old artwork long after you replaced it.
  <FilesMatch "\.(svg|png|jpe?g|webp|glb|woff2?|ico)$">
    Header set Cache-Control "public, max-age=86400"
  </FilesMatch>

  # The back office lists customers' names, addresses and phone numbers.
  SetEnvIf Request_URI "^/admin" TSHIRTLB_ADMIN
  Header set X-Robots-Tag "noindex, nofollow" env=TSHIRTLB_ADMIN
</IfModule>

# -------------------------------------------------------------------- rewrites
<IfModule mod_rewrite.c>
  RewriteEngine On

  # Force HTTPS. The second condition covers hosts that terminate TLS at a proxy,
  # where %{HTTPS} is off even though the visitor arrived over https.
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^ https://www.tshirtlb.com%{REQUEST_URI} [L,R=301]

  # Apex to www, because the app's canonical URLs use www. If both answer,
  # search engines treat them as two sites and split the ranking between them.
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^ https://www.tshirtlb.com%{REQUEST_URI} [L,R=301]

  # THE IMPORTANT ONE: serve index.html for any path that is not a real file.
  #
  # /shop, /designs/energy-saving-mode and /admin are routes the Angular router
  # draws, not files on disk. Without this, Apache looks for a file, does not
  # find one, and returns its own 404 — so the site works while you click
  # through it and 404s the moment anyone refreshes, opens a shared link, or
  # arrives from a search result. Existing files and directories are excluded so
  # real assets still serve normally.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ /index.html [L]
</IfModule>

# Directory listings would expose the whole asset tree.
Options -Indexes
