Redirect doesn't remove query params

I use the redirect configuration in netlify.toml file:

[[redirects]]
from = “/redirect”
status = 301
query = {id = “:id”, name = “:name”}
to = “/redirect/:id/:name”

and I want to redirect the user (I think it was working like this before)
from https://mysite.com?id=123&name=test
to https://mysite.com/123/test

However, it still passes the query params to the redirected site and I end up with
https://mysite.com/123/test?id=123&name=test

Is it possible to get rid of query params after a redirect?

3 Likes

Most people need those query parameters so it’s probably by design. As far as I know, you can probably only get rid of it client-side, that is using JavaScript.

The issue is that the name can contain a dot and when it’s passed as query param the page is not found. Everything is ok when the dot is not present. I can’t handle it when it’s automatically passed when redirecting. :neutral_face:

If the dot is passed as the parameter, it will redirect to the path with the dot. The only option is to not pass a dot or as I said before, you’d have to rely on JavaScript. Removing parameters on redirect won’t help with the dot, I suppose.

1 Like

It’s ok when the dot is in the URL path. However, page not found when it’s in the query params. :roll_eyes: And it’s only present on netlify. When I try it locally, everything is fine.

1 Like

This is probably possible with Netlify Functions. Trying it locally.

Everything is ok with the redirect and the query params passed over to another route. I was wrong about the issue. The issue is that netlify doesn’t accept the query params that contains the dot. This issue should be closed now.
Thank you!

It seems to work (at least locally):

Your netlify.toml:

[build]
  functions = "functions"

[[redirects]]
  status = 301
  from = "/redirect/"
  query = {id = ":id"}
  to = "/.netlify/functions/redirect/?id=:id"

Your functions/redirect.js:

exports.handler = async (event, context) => {
  return {
    statusCode: 301,
    headers: {
      Location: '/redirect/' + event.queryStringParameters.id + '/'
    }
  }
}

That looked promising, but it still keeps the query strings (re-adds them after the nice URL). My script based upon yours…

The redirect.js:

exports.handler = async (event, context) => {
    const rules = {
        kiallitasok: {
            egyeni: '/egyeni-kiallitasok/',
            'kulfoldi-egyeni': '/kulfoldi-egyeni-kiallitasok/',
            'hazai-kollektiv': '/hazai-kollektiv-kiallitasok/',
            'kulfoldi-kollektiv': '/kulfoldi-kollektiv-kiallitasok/',
            'kulfoldi-grafikai-biennalek': '/kulfoldi-grafikai-biennalek/',
        },
        eletrajz: {
            muveszeti: '/muveszeti-tevekenyseg/',
            pedagogiai: '/pedagogiai-tevekenyseg/',
            kritikak: '/meltatasok-kritikak/',
            kituntetesek: '/dijak-kituntetesek/',
        },
        alkotasok: {
            kozgyujtemenyekben: '/alkotasok-kozgyujtemenyekben/',
            kozzetett: '/kozzetett-alkotasok/',
        },
        fooldal: {
            fooldal: '/',
        },
        kapcsolat: {
            kapcsolat: '/',
        },
        publikaciok: '/publikaciok/',
        linkajanlo: '/linkajanlo/',
    };
    const kategoria = event.queryStringParameters.kategoria;
    const oldal = event.queryStringParameters.oldal;
    let loc;
    if (oldal) {
        loc = rules[kategoria][oldal];
    } else {
        loc = rules[kategoria];
    }
    return {
        statusCode: 301,
        headers: {
            Location: loc,
        },
    };
};

The netlify.toml:

[[redirects]]
  status = 301
  from = "/"
  query = {kategoria = ":kategoria", oldal = ":oldal"}
  to = "/.netlify/functions/redirect/?kategoria=:kategoria&oldal=:oldal"

[[redirects]]
  status = 301
  from = "/"
  query = {kategoria = ":kategoria"}
  to = "/.netlify/functions/redirect/?kategoria=:kategoria"

I’d like to end up with:
/muveszeti-tevekenyseg/

Where the old site has:
?kategoria=eletrajz&oldal=muveszeti

But I get:
/muveszeti-tevekenyseg/?kategoria=eletrajz&oldal=muveszeti

I tried this on production now and turns out, you’re correct, the parameters are preserved. However, they’re not preserved if you add some other parameters. You can seem the complete code here:

https://github.com/Hrishikesh-K/redirect/blob/main/functions/redirect.js

Not ideal probably, but might get the job done.

1 Like