Different backing url from DNS

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

That sounds like Domain Masking.

https://community.cloudflare.com/search?q=domain%20masking

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

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.