Redirect based on referring domain

Hi! We have domain A, it has 500 incoming links. Of these 500 links, 10 need to be redirected to domains B and the rest to domain C. I thought CF Workers might be a solution, but how do i set this up?

I found this page: https://developers.cloudflare.com/workers/examples/bulk-redirects

But this is for redirecting pages of domain A itself to another one. We need to detect the incoming links and redirect based on them. Example:

bbc.co.uk > domain A > Domain B

amazon.com > domain A > Domain C

Is this possible at all? If yes, is workers a good choice? If yes, could somebody be so kind to provide an example?
Thanks!

Yes, but referrers are generally not all too reliable. You might not get that value at all, but if you do it is relatively easy to issue that redirect based on the referrer value, which you can get from the request headers.

https://developers.cloudflare.com/workers/runtime-apis/request has all on that.

Keep in mind, Workers are paid if you exceed the free limit.

Hi,

kidnly please check Passing on referer value on 301 https redirects and there is also same discussion forked to https://stackoverflow.com/questions/55882899/how-not-to-loose-referer-value-on-301-https-redirects-in-Cloudflare-workers .

Hi Tomas, thanks for your reply. Much appreciated.

Here you are advised to set a query string parameter from domain A > B.

My problem is, I don’t have control over domain A. So I’d like to redirect based on referring domain name of domain A.

But if I understand correctly this is not possible in a reliable way?

Not possible. That is how HTTP(S) is build. For same reason when google switched to HTTPS the majority of seo data ended up being displayed as ‘not set’.

HTTP is stateless by design. You can only use cookies or query parameters (or, for that matter, custom url paths) to pass data throughout requests (not 100% sure about this statement, but essentially it should be correct).

There is a hack if you have ‘partial’ control of the origin domain - i.e. you collaborate with other domain owner to implement cross-domain data sharing. That is done by loading iframe in origin domain and using HTML5 postmessage for iframe->main window to communicate and set the cookie value which can be read by destination domain. But it’s specific use case and still a hack.

To sum up:
a) origin domain owner decidec wether to pass full path or only the domain on link clicks as referer values;
b) final call on that is being made by each users browser - which you surely have no control of.

Generally approach with referer values analysis is ‘you enjoy data you have, don’t worry about what you don’t’. And yes, that means that implementing bullet-proof redirect scenarios based on this is technically impossible.

Let me know if you have any more questions on this, would be happy to elaborate.

Then the whole point is moot as that domain cannot be on Cloudflare and Workers won’t work either.

Let me rephrase:

Many domains (A, Aa, Ab, Ac) link to domain B.

Domain B is on Cloudflare.

Incoming links on domain B should be split to domain C + D based on referring domains (A, Aa, Ab).

In which case it is what you originally described and what I already addressed yesterday. Not sure what’s not clear.

You can use the origin header.

It carries only scheme, domain name, port.
You won’t have the full path.

The origin is not necessarily available either, but if it is the handling is the same as with the referrer.

Thanks for the comments! I’m new to workers. After a few hours of trying i got here. If i link to the .dev workers page from another website (origin), i get this output:
origin: null

My code:

async function handleRequest(request) {

  let headers = request.headers;
  const requestOrigin = headers.get( "Origin" );

  return new Response( 'origin: ' +requestOrigin );

}

addEventListener("fetch", async event => {

  event.respondWith(handleRequest(event.request));

})

Please assist, highly appreciated.

I am afraid the forum here is not primarily intended for development related questions, particularly not for generic JavaScript development. StackOverflow will be better suited here. However the aforementioned link should provide you will all the details necessary to check the header and to send the redirect.

1 Like

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