I added CNAME DNS record (example.com / redirect.example.workers.dev, *.example.com / redirect.example.workers.dev) redirect the workers name.
Question:
How can I achieve the above in Netlify?
What I tried:
I added an app under backend (backend/redirect-netlify) and placed only netlify.toml and package.json.
# netlify.toml [[redirects]] from = "https://brands.example.com/*" to = "https://brand-661.pages.dev/:splat" status = 200 [[redirects]] from = "https://docs.example.com/*" to = "https://docs-cmx.pages.dev/:splat" status = 200 # Default rule for any other subdomain [[redirects]] from = "https://:subdomain.example.com/*" to = "https://consumer-1ja.pages.dev/:splat" status = 200
Netlify says the last redirect ruls is an invalid syntax.
I tried with Netlify functions. Placed functions/index.js, public/_redirects.
# functions/index.js exports.handler = async function (event, context) { const url = new URL(event.rawUrl); let split = url.hostname.split("."); if (split[0] === "www") { split = split.slice(1); } let subdomain; if (split.length >= 3) { subdomain = split[0]; } switch (subdomain) { case "brands": url.hostname = "brand.netlify.app"; break; case "docs": url.hostname = "docs.netlify.app"; break; default: const search = new URLSearchParams(url.search); if (subdomain) { search.set("brand", subdomain); } url.hostname = "consumer.netlify.app"; url.search = "?" + search.toString(); } return await fetch(new Request(url), { cf: { cacheTtl: 60, }, }); // const response = await fetch(url); // const body = response.body(); // return { // statusCode: 200, // body: body // }; }; # public/_redirects /* /.netlify/functions/index 200
This doesn’t work neither.
Side Question: Although I added domain example.com to Neltify and DNS record (*.example.com / redirect.netlify.app), https://docs.example.com shows certification error.
I’ve added a project to the monorepo, which is called rewrite. Let me share the codebase. backend/rewrite /netlify/edge-functions/rewrite.js /netlify.toml /package.json
export default (request) => { const url = new URL(request.rawUrl); const search = new URLSearchParams(url.search); let split = url.hostname.split("."); if (split[0] === "www") { split = split.slice(1); } let subdomain; if (split.length >= 4) { subdomain = split[0]; } switch (subdomain) { case "brands": url.hostname = "joltz-brand.netlify.app"; break; case "embed": url.hostname = "joltz-embed.netlify.app"; break; case "philanthrobit-referrals": url.hostname = "joltz-referral.netlify.app"; break; case "docs": url.hostname = "joltz-docs.netlify.app"; break; case "spinner": if (search.has("brand", "mrsatoshi")) { return Response.redirect("https://staging.joltz.app/spinner/e41349b1-8c15-41ec-ad7c-7c818e4d64ec", 301); } default: if (subdomain) { search.set("brand", subdomain); } url.hostname = "joltz-consumer.netlify.app"; url.search = "?" + search.toString(); } return url; }; export const config = {};
[build] command = "echo No build for this site, we are living on the edge"
**Runtime** Not set **Base directory** / **Package directory** backend/rewrite **Build command** echo No build for this site, we are living on the edge **Publish directory** Not set **Functions directory** netlify/functions **Build status** Active
But when I hit staging.joltz.app (or joltz-rewrite.netlify.app), it says Page Not Found, meaning that it’s not rewriting.
And the deploy summary says “no edge function deployed”.
I didn’t set edge function path, because I already placed it in the default path (backend/rewrite/netlify/edge-functions). Can you explain me more details what I should do? What if I remove that config object?
The site is joltz-rewrite.netlify.app and I want this to be happen: when a user hits https://joltz-rewrite.netlify.app, it goes though the edge function directly.