Worker URL Rewrite not working on

I wrote a Worker that is applied to the Worker Route: ** Its job is to rewrite: → This test page returns the Domain and Site parameter correctly: This test page does not, despite the logs (below) looking correct: (note the Site parameter is excluded and the Domain isn’t rewritten) -------------------
2 Replies
A15w ago
The console logs for my worker seem to be correct, it seems to be creating the correct URL to rewrite to. "logs": [ { "message": [ "Original URL:", "" ], "level": "log", "timestamp": 1713851318452 }, { "message": [ "Subdomain:", "test" ], "level": "log", "timestamp": 1713851318452 }, { "message": [ "New Hostname:", "" ], "level": "log", "timestamp": 1713851318452 }, { "message": [ "New URL:", "" ], "level": "log", "timestamp": 1713851318452 }, { "message": [ "New URL w/ Params:", "" ], "level": "log", "timestamp": 1713851318452 } ], -------------- And here's the Worker code: addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) }) async function handleRequest(originalRequest) { // Clone the original request to maintain immutability const request = new Request(originalRequest); // Parse the original request's URL const url = new URL(request.url); console.log('Original URL:', url); // Log the original URL // Extract the subdomain assuming 'subdomain.domain.tld' structure const subdomain = url.hostname.split('.')[0]; console.log('Subdomain:', subdomain); // Log the original URL if (subdomain === 'www' || subdomain === url.hostname) { // Do not rewrite requests for 'www' or the root domain return fetch(request); } // Remove the subdomain from the URL and prepare to add it as a query parameter const newHostname = url.hostname.replace(subdomain + '.', ''); const newUrl = new URL(${url.protocol}//${newHostname}${url.pathname}); console.log('New Hostname:', newHostname); // Log the original URL console.log('New URL:', newUrl); // Log the original URL // Add the 'site' query parameter to the URL's search parameters newUrl.searchParams.set('site', subdomain); console.log('New URL w/ Params:', newUrl); // Log the original URL // Construct a new request object with the rewritten URL const rewrittenRequest = new Request(newUrl, { method: request.method, headers: request.headers, // Preserve the body if the method isn't GET or HEAD body: (request.method !== 'GET' && request.method !== 'HEAD') ? request.body : undefined }); // Fetch and return the response for the rewritten request return fetch(rewrittenRequest); Would LOVE to know what in the world I’m doing wrong. Appreciate the help! Al Hi folks, still struggling to resolve this. Appreciate anyone who has time to look at what I've shared to maybe point out what I've done wrong. THANK YOU! Al
sebastian®2w ago
@A1 - Any movement on this? I too am looking to rewrite content, a path from a subdomain. (Proxy) This path must render content from All sub-paths of /bar/... should map as well.