# Oath Research clone — Apache / LiteSpeed (.htaccess at the site root)
#
# Every purchase route 302s to the shop this site is configured for. postprocess fills the two
# RewriteRule targets below from moduleConfig's shopUrl, shopParams and campaign, so a site the
# client pointed at its own shop redirects there server-side too, not only in JavaScript. With no
# campaign configured the value becomes %1 — the request host with a leading "www." removed.
# Also: clean URLs (/about -> about.html), the custom 404 page, and no access to
# the tooling folder in case it was uploaded by mistake.
#
# On cPanel hosts keep the "# BEGIN cPanel-generated ..." block the host adds;
# never add X-Robots-Tag / noindex headers — the clones must stay indexable.

ErrorDocument 404 /404.html
# ErrorDocument 403 would only change the body, not the status, so the two sources of 403 are
# turned into real 404s further down instead: the tooling block, and a directory with no index.
ErrorDocument 403 /404.html
DirectoryIndex index.html index.htm
Options -Indexes

# Pages, scripts and styles are revalidated on every visit (cheap 304s). Without any
# Cache-Control, browsers keep files "fresh" for hours on their own heuristics, so an
# update — above all a fix to js/oath-redirect.js — would not reach returning visitors.
# "no-cache" still allows the back/forward cache; never use "no-store" here.
<IfModule mod_headers.c>
  <FilesMatch "\.(html?|js|css|json|xml|webmanifest)$">
    Header set Cache-Control "no-cache"
  </FilesMatch>
</IfModule>

RewriteEngine On

# Tooling and notes are never served. 404 rather than 403: a "forbidden" confirms the path is
# there, and a visitor cannot act on either answer.
RewriteRule ^oath-kit(/|$) - [R=404,L]
RewriteRule ^CLAUDE(\.root)?\.md$ - [R=404,L]

# capture the bare host (strip leading www.)
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:cart|carts|checkout|checkouts|basket|bag|shopping-cart|shoppingcart|shopping-basket|order|orders|order-now|purchase|buy|buy-now|payment|payments|pay|my-account|account|wishlist)(?:/.*)?$ https://buygenerics.com/?promo=mds&campaign=highstreetpharma.com [R=302,L,NE]

# WooCommerce-style query actions (?add-to-cart=123) and restored-cart links.
# %1 comes from the LAST RewriteCond, so the host capture must be the last one.
RewriteCond %{QUERY_STRING} (?:^|&)(?:add-to-cart|add_to_cart|restore_cart)= [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://buygenerics.com/?promo=mds&campaign=highstreetpharma.com [R=302,L,NE]

# index.html never shows in the address bar (client request 2026-10-29): /x/index.html -> /x/ and
# /index.html -> /. THE_REQUEST is what the visitor asked for, so the server's own DirectoryIndex
# lookup of index.html never triggers it (no loop). Must stay above the .html rule below.
RewriteCond %{THE_REQUEST} \s/+((?:[^\s?]*/)?)index\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# Canonical URLs: a direct .html request 301s to the extension-less URL (added 2026-09-22, client
# request — Google had indexed the clean URLs and they 404'd where this rule did not exist yet).
# %{THE_REQUEST} is the literal request line, so this only fires on what was actually typed/clicked,
# never on the internal rewrite below — no redirect loop. Excludes /404.html (ErrorDocument target)
# and /index.html (directory index); the file-existence check keeps it from firing on fake paths
# like a bare "cart.html" that only exists as a directory (cart/index.html).
RewriteCond %{THE_REQUEST} \s/+(?!(?:404|index)\.html(?:[\s?]))([^\s?]+)\.html[\s?] [NC]
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^ /%1 [R=301,L,NE]

# Clean URLs for the static files: /about -> /about.html. A folder of the same name does not block
# this unless the folder has its own index.html (modadove has products.html AND a products/ folder,
# so /products 404'd while the old "!-d" test was here).
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}.html -f
# The pattern excludes a trailing slash. With ^(.+)$ a request for /404/ was rewritten to
# "404/.html" — Apache resolves REQUEST_FILENAME for /404/ to <root>/404, so the -f test found
# the real /404.html and let the rule fire with the slash still in the capture. The result was
# an HTTP 500 on every site in the batch, which is the worst thing to show for a bad URL.
# A directory-style path that does not exist should simply 404.
RewriteRule ^(.*[^/])$ $1.html [L]

# --- gaps in the crawl: do not show a bare 404 (client request, 2026-09-24) -------------
# Some crawls are missing product images or detail pages — the source had withdrawn them, or
# the crawl was cut short. A raw 404 reads as a broken site, so the two cases are handled
# differently. Both are last-resort rules: every one of them first proves the real file,
# directory and .html form do not exist, so nothing that works is touched.

# 1. A missing IMAGE serves the site's placeholder, with a 200 — NOT a redirect. An <img> whose
#    request is redirected to an HTML page still renders as a broken image; only real image
#    bytes fix it.
#    The substitution is RELATIVE on purpose. In a per-directory (.htaccess) context Apache
#    resolves it against the directory holding this file, so it finds the right placeholder
#    whether the site is served as its own subdomain (docroot = the site folder) or from a
#    sub-path of a shared docroot. An absolute /images/placeholder.png, or a
#    %{DOCUMENT_ROOT}/… test, only works in the first case.
#    Excluding the placeholder itself is what prevents a loop: after the internal rewrite the
#    ruleset runs again, and without this it would rewrite the placeholder to itself forever —
#    including on a site that has no placeholder at all.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !placeholder\.png$
RewriteRule \.(png|jpe?g|gif|webp|avif|svg)$ images/placeholder.png [L]

# 2. A missing PAGE under a content section goes to that section's index. Scoped to the
#    sections whose crawls actually have gaps: sending every unknown URL to the home page
#    would hide genuine link errors, and a crawler reads it as a soft 404. Anything outside
#    these sections still gets the custom 404 page.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html !-f
RewriteCond %{DOCUMENT_ROOT}/products.html -f
RewriteRule ^products?/ /products [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html !-f
RewriteCond %{DOCUMENT_ROOT}/reports/index.html -f
RewriteRule ^(?:reports|verify)/ /reports/ [R=302,L,NE]

# A catalogue whose product pages live under their own prefix, with the browser as the index
# (wuxitides: /catalog-product/product/<slug>/ listed by /catalog-product/browse/). The catalogue
# index is rendered from a local JSON file that names every product, so it lists far more pages
# than a crawl reaches; without this, each one a crawl missed is a bare 404 on a link the page
# itself generated.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html !-f
RewriteCond %{DOCUMENT_ROOT}/catalog-product/browse/index.html -f
RewriteRule ^catalog-product/ /catalog-product/browse/ [R=302,L,NE]

# A directory that exists but has no index page gives 403 under Options -Indexes, which reads as
# a permissions fault and confirms the path exists. Some crawls left such directories behind
# (wuxitides /about-us/ holds only subdirectories, and the source answers 404 for it too).
# Answer 404, the same as any other address with nothing at it. Last, so it can only fire
# after every rule above has declined the request.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.htm !-f
RewriteRule ^ - [R=404,L]

# Fonts / manifest MIME types (in case the host does not know them)
AddType font/woff2 .woff2
AddType font/ttf .ttf
AddType application/manifest+json .webmanifest
