NextJS , redirect , how to remove the query parameter?

Hello,

I was using the next.config.js but then I read your documentation and moved everything to netlify.toml . The redirect do work but my goal is to do this redirect:

/page?foo=bar => /page/bar

but I am getting this instead:

/page?foo=bar => /page/bar?foo=bar

What I have done today:

[[redirects]]
from = "/page"
to = "/page/:bar"
status = 301
force = true
query = {foo = ":bar"}

How can I solve this ?

Hi @leoB,

Netlify passes query parameters in redirects and there’s no option to disable that.

What would could do is change URLs with history.pushState() once the page loads.

I know I’m replying to an old discussion, but can you @hrishikesh explain why this is? It’s pretty frustrating in some situations.

For example, if I want to redirect user depending on some query parameter and simultaneously remove the query param, unless I can remove the query param I have to deal with it some other way. My current workaround is to add another query param (?redirected).

Yes, that is a way I’ve suggested in some other threads.

Mostly because that is probably the intended behaviour in most cases. If someone clicks on a Google search result, goes to HTTP page, we redirect to https and if we stripped parameters, a lot of tools like Analytics will break. A lot of users rely on the current behaviour as it seems to be intended for them.

For you, it’s probably not intended, but unfortunately, there’s no way to configure yet whether or not to preserve these parameters. Such a feature would probably solve the problems for all. In the meanwhile, you can add another query to replace the previous ones or use JavaScript.

1 Like