I'm trying to create a Cloudflare worker that redirects domains based on some input wildcard directions.
For example, let's say the input is this:
const inputs = [
{
"from": "*.example.com/products/3268-shirts*",
"to": "https://store.primarydomain.example.com/collections/3268-shirts*"
}
]
Based on this, the following should work:
[
{
"input": "https://store.domain1.example.com/collections/3222-shirts",
"output": "https://store.primarydomain.example.com/collections/3222-shirts"
},
{
"input": "https://store.domain2.example.com/collections/3222-shirt",
"output": "https://store.primarydomain.example.com/collections/3222-shirts"
},
{
"input": "https://store.domain2.example.com/collections/3222-shirts/site4",
"output": "https://store.primarydomain.example.com/collections/3222-shirts/site4"
},
{
"input": "https://store.domain2.example.com/collections/3222-shirts/?pf_t_site=the_site",
"output": "https://store.primarydomain.example.com/collections/3222-shirts/?pf_t_site=site_site"
},
{
"input": "https://store.domain2.example.com/collections/3222-shirts/products/9000-pants?variant=2995454033242234&utm_content=171134",
"output": "https://store.primarydomain.example.com/collections/3222-shirts/products/9000-pants?variant=2995454033242234&utm_content=171134"
}
]
The input would be the requested domain and the output is the domain that it'll redirect to.
Here's what I have so far for this, which is close. It redirects, but not for this specific case, and not with the correct domains.
const inputs = [
{
"from": "https://store.domain2.example.com/collections/*",
"to": "https://store.example.com/collections/*"
}
]
async function handleRequest(request) {
const {url} = request;
const location = redirects.reduce((loc, r) => {
let match = url.split(r.from.replace("*", ""))[1];
let final = r.to.replace("*", "") + match;
if (match) return final;
}, "");
if (location) {
console.log(`${url} redirects to ${location}`)
return Response.redirect(location, 301)
}
return fetch(request)
}
addEventListener("fetch", async event => {
event.respondWith(handleRequest(event.request))
})
How can I change the code above so the test urls would work?
The following should be able to get you going. Note that handleRequest() will match on the FIRST redirect.from. Thus, place the more specific ones on top if the destination should be different.
var redirects = [
{
"from": "oldstore1.example.com", // target a specific sub-domain. Notice, this is before the match all sub-domains of example.com
"to": "oldstore1.primarydomain.example.com"
},
{
"from": ".example.com", // will match any/all sub-domains
"to": "primarydomain.example.com"
}
]
// This will match on the first matching redirects.from.
function handleRequest(request) {
let visitorDomain = new URL(request.url);
const location = redirects.find((loc, r) => visitorDomain.hostname.endsWith(loc.from)); // matches on the ending of URL.hostname.
// check if either no suitable location was matched
// OR if the destination matches the current URL, if so, do not perform 301
if(!location || visitorDomain.hostname === location.to) {
console.log('Not going to perform 301');
return //fetch(request);
}
// A redirect will happen
const redirectDest = visitorDomain.protocol + '//' + location.to + visitorDomain.pathname + visitorDomain.search;
console.log('redirecting to', redirectDest);
//return Response.redirect(location, 301)
}
Some Examples
handleRequest({url: 'https://store.domain2.example.com/collections/3222-shirts'});
// redirects to: https://primarydomain.example.com/collections/3222-shirts
handleRequest({url: 'https://www.somewhere.oldstore1.example.com/collections/3222-shirts'});
// redirects to: https://oldstore1.primarydomain.example.com/collections/3222-shirts
handleRequest({url: 'https://store.domain2.example.com/collections/3222-shirts/products/9000-pants?variant=2995454033242234&utm_content=171134'});
// redirects to: https://primarydomain.example.com/collections/3222-shirts/products/9000-pants?variant=2995454033242234&utm_content=171134
handleRequest({url: 'https://store.domain2.example.com/collections/3222-shirts/products/9000-pants'});
// redirects to: https://primarydomain.example.com/collections/3222-shirts/products/9000-pants?variant=2995454033242234&utm_content=171134
handleRequest({url: 'https://primarydomain.example.com/collections/3222-shirts/products/9000-pants'});
// NOT going to Redirect. Already at a suitable matching subdomain.domain.