301 Redirects with query string colon works but question mark does not

Hi I have a issue with redirects with query string params.
I am using .toml file with this format:

[[redirects]]
from = "/installs.html?id=16"
to = "/installs/#cup-holder"
status = 301
force = true

While all other redirects work the ones with olddomain.com/installs?id=NUMBER redirect to the to newdomain.com/?id=NUMBER instead of newdomain.com/installs/cupholder/

Yet if I change the ? to : then the redirect works and goes to the right place

[[redirects]]
from = "/installsDetail.html:id=16"
to = "/installs/#cup-holder"
status = 301
force = true

However the old indexed url uses a ? with the query string.
I spent hours researching and nothing works except the colon. The documentation for query strings is sort of vague. Wish there few examples where you can choose between _redirects and netlify.toml as in some the redirect examples show. The query string example seems kinda odd to me /store id=:id /blog/:id 301
id=:id??? how does that even work?

@Roo416 The examples given in the documentation for query string parameters are in the format for the _redirects file, (not the netlify.toml file), so you would need to translate them accordingly.

In your case I believe you want something along the lines of…

[[redirects]]
force = true
from = "/installsDetail.html"
query = {id = ":id"}
status = 301
to = "/installs/#cup-holder"

@nathanmartin Thank you for your reply. I have tried that several ways and still getting the same result. Here is what I have tried:

At first tested the basic thinking that the question mark will be replaced with colon but that didn’t work

[[redirects]]
from = "/installsDetail.html"
to = "/porsche/#cup-holder"
query = {id = ":id"}
status = 301
force = true

Then I tried to use the full id

[[redirects]]
from = "/installsDetail.html"
to = "/porsche/#cup-holder"
query = {id = ":id=16"}
status = 301
force = true

Then tried a variety of formats but nothing works

query = {?id = ":id"} // build error
query = {id = ":id = 16"}
query = {id = ":id16"}
query = {id = ":16"}

All of these tests ended up with the same result as I mentioned in the opening question.

Seems like this is the only way it works

from = "/installsDetail.html:id=16"

I must be missing something here.

Figured several things here.
First I had this redirect to catch all regardless of query string

[[redirects]]
from = "/installsDetail.html"
to = "/"
status = 301
force = true

Then I was getting specific with certain query strings

[[redirects]]
from = "/installs.html?id=16"
to = "/installs/#cup-holder"
status = 301
force = true

So what was happening was the redirect hit the first one and redirected with the query sting but to the home page. I rearranged the redirects with the specific first followed by the catch all and the redirect worked which makes me believe that more specific redirects need to come first.

However now anything specific that follows doesn’t work as the redirect hits the first specific one (id=16 ) and redirects there even though the url was going for id=17. Getting closer though

[[redirects]]
from = "/installsDetail.html"
query = {id = ":id=16"}
to = "/porsche/#cup-holder"
status = 301
force = true

[[redirects]]
from = "/installsDetail.html"
query = {id = ":id=17"}
to = "/porsche/#subwoofer"
status = 301
force = true

Query string redirects cannot be used to redirect based on a specific value, they’re used to check the presence of the query parameter and pass their value to the destination. You would have to use Edge Functions for that.

1 Like