Worker redirect with cname on 3rd party domain

First post, I have a worker script that redirects a set of paths on a specific cname (to a 3rd party) to another cname and path on my domain. It’s based on the redirect script template. It works in the sandbox, it does not work live. Is there something unique to redirecting requests originating to a cname of a third party?

By redirect you mean a regular 30x response? In that case it really shouldnt matter if the target host is a CNAME or A or AAAA record. That will all be handled by the browser anyhow.

Can you post your code?

connect.hideawayreport. com is a cname to hubspot subdomain. I want to 301 redirect some but not all requests to connect.hideawayreport. com to www.hideawayreport. com/join

…I had to make the above URLs not look like URLs since new users can not post more than 2 links per post

async function handleRequest(request) {

let requestURL = new URL(request.url)
if(requestURL.hostname == ‘connect.hideawayreport.com’){
let location = redirectMap.get(requestURL.pathname)
if (location) {
return Response.redirect(location, 301)
}
}else{
console.log(‘hostname does not match’)
}
// If in map, return the original request
return fetch(request)
}
addEventListener(‘fetch’, async event => {
event.respondWith(handleRequest(event.request))

})

const externalHostname = ‘www.hideawayreport.com
const redirectMap = new Map([
[’/gift’, ‘https://’ + externalHostname + ‘/join’]
/* truncated the rest of the list in order to post */ ])

connect.hideawayreport.com appears to be non-proxied, hence workers wont run.

You should switch that record to :orange:.

interesting. I had switched that to proxied about an hour ago and it didn’t take the change. I’ve tried a couple times since your reply and the setting will not stay proxied. Does this have something to do with connect’s content value being a subdomain on hubspot.net

That is correct. Actually you should not even be able to switch it in the first place. That address already resolves to a Cloudflare address and hence you cannot re-proxy it again. Thats a known limitation of Cloudflare.

What you could do instead is use a dummy A record (very much like for the “www” redirects under #tutorials) and handle the rest yourself.

basically…

  1. Connect would become an A www.
  2. I would create a new cname for hubspot, ‘hs’, perhaps.
  3. The worker would redirect connect traffic to hs unless it was in the redirectMap, in which case it would redirect to www.hideawayreport.com/join.

correct?

That might work too. I am not familiar with your redirection requirements, so its a bit hard to tell, however the bottom line is you need to make sure the requests are proxied, otherwise the worker wont execute.

1 Like

got it! thank you for your help