Issue from subdomains to subdirectory

Hi!

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 -


https://www.dotcept.com/blog

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: "blog.dotcept.com",
  //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)
  
  console.log(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.host, parsedUrl.pathname);
  // if its not a request blog related stuff, do nothing
  return fetch(request)
}

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})

What’s your worker route? I guess you only configured routing for www.dotcept.com/blog/*

So here’s what happened:

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

image

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

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