WILDCARD REDIRECTS

I was trying to setup wildcard redirects for subdomain which has no dns records via cloudflare workers with the route *is-a.software/*but is it also redirecting those domains which already has a record and are proxied i tried to write a logic via a api which returns list of all registered subdomains which is https://raw.is-a.software/domains.json and the code looks like this
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const hostname = url.hostname;

if (hostname === "is-a.software" || hostname === "www.is-a.software") {
return fetch(request);
}

try {
const subdomain = hostname.replace(/\.is-a\.software$/i, "");


const res = await fetch("https://raw.is-a.software/domains.json", {
headers: { "User-Agent": "is-a-worker" },
cf: { cacheTtl: 60 },
});

if (!res.ok) {
return new Response("Failed to fetch domain list.", { status: 502 });
}

const domains = await res.json();

const registered = domains.some(
(entry) =>
entry.domain &&
entry.domain.toLowerCase() === subdomain.toLowerCase()
);

if (registered) {
return fetch(request);
}

let asset = await env.ASSETS.fetch(request);


if (asset.status === 404) {
asset = await env.ASSETS.fetch(
new Request("https://is-a.software/index.html", request)
);
}

return asset;
} catch (err) {
return new Response("Internal error: " + err.message, {
status: 500,
});
}
},
};
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const hostname = url.hostname;

if (hostname === "is-a.software" || hostname === "www.is-a.software") {
return fetch(request);
}

try {
const subdomain = hostname.replace(/\.is-a\.software$/i, "");


const res = await fetch("https://raw.is-a.software/domains.json", {
headers: { "User-Agent": "is-a-worker" },
cf: { cacheTtl: 60 },
});

if (!res.ok) {
return new Response("Failed to fetch domain list.", { status: 502 });
}

const domains = await res.json();

const registered = domains.some(
(entry) =>
entry.domain &&
entry.domain.toLowerCase() === subdomain.toLowerCase()
);

if (registered) {
return fetch(request);
}

let asset = await env.ASSETS.fetch(request);


if (asset.status === 404) {
asset = await env.ASSETS.fetch(
new Request("https://is-a.software/index.html", request)
);
}

return asset;
} catch (err) {
return new Response("Internal error: " + err.message, {
status: 500,
});
}
},
};
and the wrangle.toml name = "unregistered" main = "worker.js" compatibility_date = "2025-11-02" [assets] directory = "public" binding = "ASSETS"
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?