Set up a Sub Directory Blog using Cloudflare and Feather

It’s super easy to create a sub directory blog using Feather and Cloudflare Workers

Meta Description
Meta Title

Creating the Worker Service

1. Open Workers Dashboard

notion image

2. Create a Service

notion image
You can name this whatever you want
notion image

3. Edit your worker

notion image

4. Add the following code

notion image
Please make sure to change the domain to your blog domain in line 16 as shown in the above screenshot.
addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const urlObject = new URL(request.url)

  if (urlObject.pathname.endsWith('/') && urlObject.pathname !== '/') {
    urlObject.pathname = urlObject.pathname.slice(0, -1)
    return Response.redirect(urlObject.toString(), 301)
  }

  const host = urlObject.host

  try {
    const blogHost = "my-blog.feather.blog"
    const paths = ["blog", "_feather"]

    let url = new URL(request.url)

    const prevUrl = new URL(url)    
    
    if (
      paths.some((path) => urlObject.pathname === `/${path}` || 
      urlObject.pathname.startsWith(`/${path}/`))
    ) {        
      url.hostname = blogHost
    }

    let proxyRequest = new Request(url.toString(), request)
    proxyRequest.headers.set("X-Forwarded-Host", host)

    return fetch(proxyRequest)
  } catch (e) {    
    return await fetch(request);
  }
}
After adding the above code, click on “Save and Deploy”.

5. Go to Triggers and add the worker routes

notion image
 
We need two routes:
  1. /blog*
  1. /_feather*
 
For example, if you are trying to set up a subdirectory blog for feather.so domain, you would select the feather.so zone and add the required routes as shown in the below screenshots.
notion image
notion image

6. Setup Subdirectory in Feather dashboard

Go to Settings > Database Settings and then set the URL Prefix of all the things to blog.
notion image