How to configure Sub-folders with Cloudflare on Feather

Cloudflare configuration setup to host your blog as a /blog sub-folder

Published on:

How to configure Sub-folders with Cloudflare on Feather
Do not index
Do not index
This is the most robust and easiest way to add Sub-folders. Any type of website can be configured to use sub-folders when your DNS records are hosted on Cloudflare. If you are not using Cloudflare as your DNS, I suggest you to use it. Around 20% of the website are powered by Cloudflare.
Let’s get started with the setup,

1. Open Workers Dashboard

 
notion image

2. Create a service

notion image
You can name this whatever you want.
notion image
You cannot edit the worker code yet so simply press finish.
notion image
You should now see your worker in the workers overview page.
notion image

3. Edit your worker

Click on your worker and press edit code.
notion image
Remove all the existing code and paste the following code into the worker.
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 = "mydomain.feather.blog" // change this to your feather blog url
    
    // I'm hosting on 'mydomain.com/blog' that's why I have ["blog", "_feather"] in below code
    // if you're hosting on 'mydomain.com/help' then below code should be ["help", "_feather"]
    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);
  }
}
💡
Please make sure to update line 16 & line 20 accordingly.
Once you paste it, your worker should look like this. Make the changes as required and then press deploy.
notion image

4. Set trigger routes

notion image
We need 2 routes
  1. /blog* (if you are hosting on mydomain/help, you will need to write /help* instead)
  1. /_feather*
Let’s consider here that you’re trying to host Sub-folder at /blog for ecomreco.com domain. For that, you would have to select ecomreco.com zone and add the required routes as shown in the below screenshots.
notion image
notion image

5. Setup Sub-folder in Feather Dashboard

In Feather, navigate to Settings > Features and switch on “Enable Subdirectory” feature and after that go to Settings > Sub-folder management and enter the following values,
notion image
💡
Make sure to replace www.mydomain.com with the domain you are using and blog with whatever sub-route you plan to use.
 
Important Note: Under the Domains section make sure that you are using Subdomain settings. If you have added a custom domain there, please remove it. Sub-folders does not require the Custom Domain configurations and if you have it, then the sub-folder will not work.
 
Once you do these, your sub-folder should be live. You should be able to access your feather site on www.mydomain.com/blog. If it’s not reach out to us at support@feather.so.