Wildcard Worker + Route

I have a website which is using two separate Content Management Systems - one is for sales and the other is Wordpress - so they’re currently running on two different domains. As the site is on an NDA at the moment I can’t share the specific URLs right now but essentially, they’re set up like so:

Sales site: www.websitename.com
Wordpress site: subdomain.websitename.com

So the sales site is sitting on the main domain www and the wordpress is pointing to a subdomain.

What I need to do, however, is have both sides of the site showing under the www domain. It’s my understanding I can use workers and routes to achieve this, but I’m unsure of the correct way to set this up.

Essentially, I want to route any pages on the subdomain to be accessible via the www domain. So, for example:

subdomain.websitename.com/blog could be viewed on www.websitename.com/blog

But also any posts and pages under that path too, so for example:

subdomain.websitename.com/blog/this-is-a-post could be viewed on www.websitename.com/blog/this-is-a-post

So, really, I need a worker and route that would work with a wildcard to manage all permutations of this domain EXCEPT the base URL itself, so I don’t want subdomain.websitename.com to display differently, but I do want everything else to change: subdomain.websitename.com.com/*

I’ve managed to do a trigger route that works with one URL, but I can’t figure out how to do it for all URLs without breaking everything:

addEventListener("fetch", event => {

event.respondWith(handleRequest(event.request));

});

async function handleRequest(request) {

// Construct the target URL

const targetBaseURL = 'https://subdomain.websitename.com/blog/';

//const targetURL = targetBaseURL + new URL(request.url).pathname;

const targetURL = targetBaseURL;

// Fetch the content from the target URL

const response = await fetch(targetURL, request);

console.log(targetURL);

console.log(response);

const newResponse = new Response(response.body, {

status: response.status,

statusText: response.statusText,

headers: response.headers

});

return newResponse;

}

Does that make sense? It’s quite complicated and difficult to explain but the reason for doing this is because the website was previously on one CMS, but is being split onto two because the sales CMS doesn’t have any capabilities for blogs or news and so we still need a CMS like Wordpress, but we need everything to show under the one domain for SEO reasons.

Any help would be appreciated.