A1
CDCloudflare Developers
•Created by A1 on 4/23/2024 in #workers-help
Worker URL Rewrite not working on Bubble.io
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
5 replies
CDCloudflare Developers
•Created by A1 on 4/23/2024 in #workers-help
Worker URL Rewrite not working on Bubble.io
--------------
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!
Al5 replies
CDCloudflare Developers
•Created by A1 on 4/23/2024 in #workers-help
Worker URL Rewrite not working on Bubble.io
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
}
],
5 replies