From 52c22d7fb07366615a808083679613bbb14f1052 Mon Sep 17 00:00:00 2001 From: baalajimaestro Date: Sun, 14 Jul 2024 19:18:23 +0530 Subject: [PATCH] Redirect automatically if the site isnt running on the correct url Signed-off-by: baalajimaestro --- src/middleware.ts | 18 ++++++++++++++++++ src/site-config.ts | 1 + 2 files changed, 19 insertions(+) create mode 100644 src/middleware.ts diff --git a/src/middleware.ts b/src/middleware.ts new file mode 100644 index 0000000..ece7ba3 --- /dev/null +++ b/src/middleware.ts @@ -0,0 +1,18 @@ +import { defineMiddleware } from "astro:middleware"; +import siteConfig from "./site-config"; + +export const onRequest = defineMiddleware((context, next) => { + const { url, request } = context; + + if (import.meta.env.PROD) { + const configuredUrl = new URL(siteConfig.siteUrl); + + if (url.hostname !== configuredUrl.hostname) { + const newUrl = new URL(url.pathname + url.search, configuredUrl); + + return Response.redirect(newUrl.toString(), 301); + } + } + + return next(); +}); diff --git a/src/site-config.ts b/src/site-config.ts index 1bb3eed..00ecf01 100644 --- a/src/site-config.ts +++ b/src/site-config.ts @@ -4,6 +4,7 @@ export const siteConfig = { subtitle: 'DevOps Engineer and Networking Enthusiast.', description: 'Personal Website & Blog for baalajimaestro', email: 'baalajimaestro@ptr.moe', + siteUrl: 'https://baalajimaestro.ptr.moe', header: { logo: { src: '/images/favicon.png',