Redirect automatically if the site isnt running on the correct url

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2024-07-14 19:18:23 +05:30
parent 7a4335a986
commit 52c22d7fb0
Signed by: baalajimaestro
GPG key ID: F93C394FE9BBAFD5
2 changed files with 19 additions and 0 deletions

18
src/middleware.ts Normal file
View file

@ -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();
});

View file

@ -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',