Redirect in Worker from *domain.com/* not using ?param=* in URL

In the function redirect, I have

const path = url.pathname
const redirectURL = serverLocation + FQDN
const destinationURL = protocol + "//" + redirectURL + path
console.log('redirecting to '+destinationURL)
return Response.redirect(destinationURL, 301)

From the logs I found the following

"logs":
    [ {
        "message": [ "redirecting to https://cdn.domainname/article.php" ], "
        level": "log",
    }],
 "event": {
    "request": {
        "url": "https://domainname/article.php?storyid=123456",
        "method": "GET",
    },

As seen in the event.request.url, the URL had article.php?storyid=123456. After the redirect was executed in Worker, as seen in the message, the partial url.pathname got lost.

It appears that I have to modify in the Router, which is now star domain.com/ star, to something else. Any suggestions to modify the Regex in domain.com/ to capture and use it for redirect?

This is correct behaviour. pathname does not include query params, it is simply the path.
The search property is what you want - URL.search - Web APIs | MDN

1 Like

Hi Walshy,

Thanks. Thats the answer. I got it. :heart_eyes:

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