Worker/redirect rule conflict

Hi there. The goal: Rewrite URL so that "mydomain.com/asdf" shows what is hosted at "otherdomain.com/asdf". For all other traffic to "mydomain.com" that does not include "/asdf", redirect to "otherdomain.com/otherpage". The problem: Everything is working correctly, except - if the URL "mydomain.com/asdf" does not include a trailing slash, the page is redirected, but it should be rewritten. If it does include a trailing slash, "mydomain.com/asdf/", then it rewrites correctly. I've got a worker which rewrites all URLs containing "mydomain.com" and URI path containing "asdf" by fetching "otherdomain.com/asdf". I've also got a redirect rule which redirects all traffic to "mydomain.com" except for "mydomain.com/asdf*". Cloudflare support confirmed that the redirect rule is set up correctly, and sent me here to get the Worker script looked at. Worker script
addEventListener("fetch", event => {
let requestURI = new URL(event.request.url);

if (requestURI.hostname.includes("mydomain.com") && requestURI.pathname.toLowerCase().includes("asdf")) {
event.respondWith(rewriteURL(event.request));
}
})

async function rewriteURL(request) {
var URI = new URL(request.url);
URI.protocol = "https";
URI.hostname = "www.otherdomain.com";
URI.pathname = URI.pathname;

return fetch(URI, request);
}
addEventListener("fetch", event => {
let requestURI = new URL(event.request.url);

if (requestURI.hostname.includes("mydomain.com") && requestURI.pathname.toLowerCase().includes("asdf")) {
event.respondWith(rewriteURL(event.request));
}
})

async function rewriteURL(request) {
var URI = new URL(request.url);
URI.protocol = "https";
URI.hostname = "www.otherdomain.com";
URI.pathname = URI.pathname;

return fetch(URI, request);
}
Redirect Rule (on mydomain.com)
(not lower(http.request.full_uri) contains "mydomain.com/asdf") and (http.host eq "mydomain.com")
(not lower(http.request.full_uri) contains "mydomain.com/asdf") and (http.host eq "mydomain.com")
This is a Static redirect to https://otherdomain.com/otherpage/ with 301 status code.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server