Wordpress with NodeBB as subdomain

Hi there,

I just signed up with Cloudflare today. My main website (example.com) is a wordpress site. I also have a forum done in nodeBB on a different server (forum.example.com). For SEO purposes, I’d like to change the forum to example.com/forum.
Fairly simple, right?

I follow & use the same script from this article and : https://blog.cloudflare.com/subdomains-vs-subdirectories-improved-seo-part-2/

// keep track of all our forum endpoints here
const myForum = {
hostname: "forum.example.com",
targetSubdirectory: "/forum",
assetsPathnames: ["/plugins/", "/assets/", "/api/", "/socket.io/"]

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 forum html, get it
  if (requestMatches(myForum.targetSubdirectory)) {
console.log("this is a request for a forum document", parsedUrl.pathname)
const targetPath = formatPath(parsedUrl)

return fetch(`https://${myForum.hostname}/${targetPath}`)
  // if its forum assets, get them
  if (myForum.assetsPathnames.some(requestMatches)) {
console.log("this is a request for forum assets", parsedUrl.pathname)
const assetUrl = request.url.replace(parsedUrl.hostname, myForum.hostname)
return fetch(assetUrl)    

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

addEventListener("fetch", event => {

When I type example.com/forum, the browser shows the correct content. But when I click one of the link (eg: /categories), the URL changed to example.com/categories. It still shows the correct content. However, when I type example.com/categories into the address bar, it gives 404 error from Wordpress as expected.

How do I make all the links on the forum relative to the URL (eg: example.com/forum/link-to-the-post instead of example.com/link-to-the-post) ?

Thanks in advance!