Worker URL Rewrite not working on Bubble.io
I wrote a Worker that is applied to the Worker Route:
*.aholics.com/*
Its job is to rewrite: domain.aholics.com → aholics.com/?site=domain
This test page returns the Domain and Site parameter correctly:
https://aholics.com/?site=test
This test page does not, despite the logs (below) looking correct:
https://test.aholics.com
(note the Site parameter is excluded and the Domain isn’t rewritten)
-------------------2 Replies
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:",
"https://test.aholics.com/"
],
"level": "log",
"timestamp": 1713851318452
},
{
"message": [
"Subdomain:",
"test"
],
"level": "log",
"timestamp": 1713851318452
},
{
"message": [
"New Hostname:",
"aholics.com"
],
"level": "log",
"timestamp": 1713851318452
},
{
"message": [
"New URL:",
"https://aholics.com/"
],
"level": "log",
"timestamp": 1713851318452
},
{
"message": [
"New URL w/ Params:",
"https://aholics.com/?site=test"
],
"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@A1 - Any movement on this? I too am looking to rewrite content, a path from a subdomain. (Proxy)
This path
domain.com/bar/1
must render content from foo.domain.com/bar/1
. All sub-paths of /bar/...
should map as well.