Rewriting subdomain.example.com to example.com/subdomain

I am deploying an Astro site with middleware and want to rewrite test.example.com to example.com/test, however it returns a 522 error.
import type { MiddlewareHandler } from "astro";

export const onRequest: MiddlewareHandler = async (context, next) => {
const { request, locals } = context;

// Skip non-GET requests or if the URL matches the skip pattern
if (request.method !== "GET") return next();

const SKIP_PATTERN = /^(?:\/_image\/|\/_astro\/|[/\w-]+\.[\w-]+$)/;
if (SKIP_PATTERN.test(context.url.pathname)) return next();

const frontendUrl = locals.runtime.env.FRONTEND_URL;
const host = context.url.host;

// Handle subdomain extraction
let subdomain: string | undefined = undefined;
if (host.endsWith(".localhost:1234")) {
const parts = host.replace(":1234", "").split(".");
subdomain = parts.length > 1 ? parts[0] : undefined;
} else if (host.endsWith(frontendUrl)) {
const parts = host.split(".");
subdomain = parts.length > 2 ? parts[0] : undefined;
}

console.log("Subdomain:", subdomain);

// If no subdomain, continue with the original request
if (!subdomain) return next();

// Rewrite the URL to include the subdomain path
const newUrl = `${context.url.protocol}//${frontendUrl}/${subdomain}${context.url.pathname}${context.url.search}`;
console.log("Rewriting to:", newUrl);

const response = await fetch(newUrl);
return response;
};
import type { MiddlewareHandler } from "astro";

export const onRequest: MiddlewareHandler = async (context, next) => {
const { request, locals } = context;

// Skip non-GET requests or if the URL matches the skip pattern
if (request.method !== "GET") return next();

const SKIP_PATTERN = /^(?:\/_image\/|\/_astro\/|[/\w-]+\.[\w-]+$)/;
if (SKIP_PATTERN.test(context.url.pathname)) return next();

const frontendUrl = locals.runtime.env.FRONTEND_URL;
const host = context.url.host;

// Handle subdomain extraction
let subdomain: string | undefined = undefined;
if (host.endsWith(".localhost:1234")) {
const parts = host.replace(":1234", "").split(".");
subdomain = parts.length > 1 ? parts[0] : undefined;
} else if (host.endsWith(frontendUrl)) {
const parts = host.split(".");
subdomain = parts.length > 2 ? parts[0] : undefined;
}

console.log("Subdomain:", subdomain);

// If no subdomain, continue with the original request
if (!subdomain) return next();

// Rewrite the URL to include the subdomain path
const newUrl = `${context.url.protocol}//${frontendUrl}/${subdomain}${context.url.pathname}${context.url.search}`;
console.log("Rewriting to:", newUrl);

const response = await fetch(newUrl);
return response;
};
I'm guessing it's getting stuck in a loop or something? Not sure how to handle this properly
3 Replies
Amos
AmosOP3w ago
O didn't know. Any recommendations on how to do this instead then? So I would need 2 workers for this Yeah it's Astro middleware
Amos
AmosOP3w ago
Basically trying to mimic something like this https://github.com/vercel/platforms/blob/main/middleware.ts
GitHub
platforms/middleware.ts at main · vercel/platforms
A full-stack Next.js app with multi-tenancy and custom domain support. Built with Next.js App Router and the Vercel Domains API. - vercel/platforms
Amos
AmosOP3w ago
I guess I can just do 2 workers
Want results from more Discord servers?
Add your server