DEV Community

Derek Nguyen
Derek Nguyen

Posted on

Netlify redirects with query params don't work? Check shadowing

I want to redirect from this url...

https://example.com/page?id=abc
Enter fullscreen mode Exit fullscreen mode

To this:

https://example.com/page/abc
Enter fullscreen mode Exit fullscreen mode

Netlify allows you to redirects based on query params, hooray! Here's an example from the docs:

/store id=:id  /blog/:id  301
# /store?id=my-blog-post to /blog/my-blog-post with a 301 redirect
Enter fullscreen mode Exit fullscreen mode

So, this would just work, right?

/page id=:id  /page/:id 301
Enter fullscreen mode Exit fullscreen mode

No. No it wouldn't.

It turns out 'shadowing' is a thing. In short, Netlify won't accept the rule if there's already a static asset at the target address.

# working
/page id=:id  /page/:id 301!
Enter fullscreen mode Exit fullscreen mode

This baffles me a little bit, because supposedly, 301 is the default rule and these all works fine:

# /b is a static page
/a   /b
Enter fullscreen mode Exit fullscreen mode

Also, seeing Netlify being mostly known as a static site host, I'd expect redirecting into existing static pages would be the majority of the cases.

Anyway, I wasted 30' on this... should I have just learn to read the docs better? Maybe. At least I hope this helped other fellows who can't read docs.

Top comments (0)