Host normalisation in hooks, before routing
Serving the same content on the apex and the www subdomain splits link equity and doubles the crawl budget spent on identical pages. Check found-canonical-host
import { redirect, type Handle } from "@sveltejs/kit";
const CANONICAL_HOST = "www.example.com";
// hooks.server.ts runs before routing, so this applies to every request
// including static assets and endpoints. No framework here gives you a
// cleaner place for it.
export const handle: Handle = async ({ event, resolve }) => {
const host = event.request.headers.get("host");
if (host && host !== CANONICAL_HOST && !host.startsWith("localhost")) {
redirect(301, `https://${CANONICAL_HOST}${event.url.pathname}${event.url.search}`);
}
return resolve(event);
};