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?
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.
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.
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));
})
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.