Hey.
I’ve got a usecase where I would like to front the domain ccc but get the data from aaa.bbb. Is this possible using Cloudflare? From what I’ve found, it looks like I can easily setup a redirect, but I’d like the user to stay on the ccc domain.
For example, a request to www.ccc.com/myPage would fetch the page from aaa.bbb.com/myPage , without doing a redirect.
Thanks, hoping for the best!
// Eric
sdayman
December 16, 2020, 10:11pm
2
michael
December 17, 2020, 1:18am
3
If you have an enterprise plan, you can use a Host Header Override page rule.
2 Likes
If you are not using Enterprise plan, you can use Cloudflare Workers instead.
Hey.
Thanks for your responses. What I was after was exactly domain masking, but since enterprise is a bit outside my budget, I started messing around with workers and arrived at this frankensteins monster of the starter examples. I figured I’d share for posterity.
const target = "https://hidden.website.com/"
async function gatherResponse(response) {
const { headers } = response
const contentType = headers.get("content-type") || ""
if (contentType.includes("application/json")) {
return JSON.stringify(await response.json())
}
return await response.text()
}
async function handleRequest(request) {
const url = new URL(request.url)
const { pathname, search, hash } = url
const destinationURL = target + pathname + search + hash
const destinationResponse = await fetch(destinationURL)
const results = gatherResponse(destinationResponse)
const responseHeaders = {
headers: {
"content-type": destinationResponse.headers.get('content-type')
}
}
return new Response(await results, responseHeaders)
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
Thanks for your tips and pointers, and take care!
Best regards,
Eric
1 Like
BTW, no idea how this will work with POSTs or PUTs, haven’t tested that far yet. I’ll have to get back to you…
Ok, so last time now.
Here is a snippet that will work with different verbs and won’t strip most of the request information…
const target = "https://target.website.com/"
async function handleRequest(request) {
const url = new URL(request.url);
const { pathname, search, hash } = url;
const destinationURL = target + pathname + search + hash;
const modifiedRequest = new Request(destinationURL, {
body: request.body,
headers: request.headers,
method: request.method,
redirect: request.redirect
});
return await fetch(modifiedRequest);
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
2 Likes
system
Closed
December 18, 2020, 8:08am
9
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.