Proxy subfolder to subdomain with worker


#1

I am trying to setup a worker script that will redirect my subfolder request to a subdomain, for example www.example.com/test/ to test.example.com/

The code that I have is this:

let url = new URL(request.url);
url.hostname = url.hostname.replace(/www./, ‘test.’)
return fetch(url)

The problem is that all of the css and js scripts are then trying to load from https://www.example.com/css/app.css instead of https://www.example.com/test/css/app.css because they are relative links.

How would I handle this without creating a /test/ folder on my subdomain?