Issue from subdomains to subdirectory


I just follow this post (SEO Best Practices with Cloudflare Workers, Part 2: Implementing Subdomains) for connecting subdomain to subdirectory. Its work well for the worker, but doesn’t work after routing -

Anyone can provide some tips? thank you so much!

Can share your Workers script?

Thanks Eric! Here you go -

// keep track of all our blog endpoints here
const myBlog = {
  hostname: "",
  //targetSubdirectory: "/welcome/",
  assetsPathnames: ["/public/", "/assets/"]

async function handleRequest(request) {
  // returns an empty string or a path if one exists
  const formatPath = (url) => {
    const pruned = url.pathname.split("/").filter(part => part)
    return pruned && pruned.length > 1 ? `${pruned.join("/")}` : ""
  const parsedUrl = new URL(request.url)
  const requestMatches = match => new RegExp(match).test(parsedUrl.pathname)
  // if its blog html, get it
  //if (requestMatches(myBlog.targetSubdirectory)) {
  console.log("this is a request for a blog document", parsedUrl.pathname)
  const targetPath = formatPath(parsedUrl)
  return fetch(`https://${myBlog.hostname}${parsedUrl.pathname}`)
  // if its blog assets, get them
  if ([myBlog.assetsPathnames].some(requestMatches)) {
    console.log("this is a request for blog assets", parsedUrl.pathname)
    const assetUrl = request.url.replace(parsedUrl.hostname, myBlog.hostname);

    return fetch(assetUrl)

  console.log("this is a request to my root domain",, parsedUrl.pathname);
  // if its not a request blog related stuff, do nothing
  return fetch(request)

addEventListener("fetch", event => {

What’s your worker route? I guess you only configured routing for*

So here’s what happened:

  1. You have some assets which are located in /assets folder
  2. When your browser is querying*****, error 404 will be returned
  3. That’s because there’s no route configured for* and*


Try adding new routes for URLs mentioned in item number 3.
Or, route the entire* to your Workers script.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.