fix(seo): redirect non-canonical domains to www.mnqcatering.com
mnqeventos.es (and the bare mnqcatering.com apex) were serving the same content as www.mnqcatering.com with no redirect, causing duplicate content. Add middleware that 301-redirects any request whose host isn't exactly www.mnqcatering.com to the canonical domain, preserving path and query. Still needs verification at the reverse-proxy/hosting layer, since host detection here depends on how the Host header arrives in production.
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
import { defineMiddleware } from 'astro:middleware';
|
||||||
|
|
||||||
|
const CANONICAL_HOST = 'www.mnqcatering.com';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SEO: this site is undesirably mirrored at mnqeventos.es (same content, no
|
||||||
|
* redirect at the DNS/hosting level). This middleware enforces the canonical
|
||||||
|
* host at the application layer for any host that isn't exactly
|
||||||
|
* www.mnqcatering.com (covers mnqeventos.es, www.mnqeventos.es, and the bare
|
||||||
|
* mnqcatering.com apex).
|
||||||
|
*
|
||||||
|
* NOTE: this must also be verified at the reverse-proxy/hosting layer — host
|
||||||
|
* detection here depends on how the Host header (or X-Forwarded-Host, when
|
||||||
|
* behind a proxy) actually arrives in production. If the proxy doesn't
|
||||||
|
* forward the original host, this check will see the wrong value.
|
||||||
|
*/
|
||||||
|
export const onRequest = defineMiddleware((context, next) => {
|
||||||
|
const requestHost = context.url.host;
|
||||||
|
|
||||||
|
if (requestHost !== CANONICAL_HOST) {
|
||||||
|
const redirectUrl = new URL(context.url.pathname + context.url.search, `https://${CANONICAL_HOST}`);
|
||||||
|
return context.redirect(redirectUrl.toString(), 301);
|
||||||
|
}
|
||||||
|
|
||||||
|
return next();
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user