Redirect rule in netlify.toml format

Hi, thanks for the great free product. I have read this piece of documentation and can see the examples written in shell format, but would like to understand how to write the following in netlify.toml format:

The following match redirects a URL like: /store?id=my-blog-post to /blog/my-blog-post with a 301 redirect. This affects request URLs with an id query parameter only.

/store id=:id  /blog/:id  301

I thought it might be this:

[[redirects]]
from = "/blackpool-hotels-with-parking/blackpool-(beach)?id= tgblk239"
to = "/blackpool-hotels-with-parking/blackpool-(beach)-:id"
query = {id = ":id"}
status = 301

…as the new url is: /blackpool-hotels-with-parking/blackpool-(beach)-tgblk239, but the toml rule I wrote results in this url: /blackpool-hotels-with-parking/blackpool-(beach)-?id=tgblk239

Also, if possible could you include code examples in both flavours?

Hi @morganfeeney,

The rule should probably look like:

[[redirects]]
  from = "/blackpool-hotels-with-parking/blackpool-(beach)?id=:id"
  to = "/blackpool-hotels-with-parking/blackpool-(beach)-:id"
  query = {id = ":id"}
  status = 301
  #force = true

You need to specify :id in the source as well as the destination.

Hey @hrishikesh thanks for getting back to me. OK, I have 30 pages that have a similar url structure, so is there a way to match them all with 1 rule, I was thinking this could work:

[[redirects]]
from = "/blackpool-hotels-with-parking/*?id=:id"
to = "/blackpool-hotels-with-parking/:splat-:id"
status = 301
query = {id = ":id"}

This probably would not work because with * matches everything as it is and it ignores the string after *. So, it will also ignore the ?id parameter.

You can instead use placeholders:

[[redirects]]
  # one of the following should work
  from = "/blackpool-hotels-with-parking/:path?id=:id"
  #from = "/blackpool-hotels-with-parking/:path/?id=:id"
  to = "/blackpool-hotels-with-parking/:path-:id"
  status = 301
  query = {id = ":id"}

Hey @hrishikesh I applied the rule we discussed last and the urls ended up like this:

/blackpool-hotels-with-parking/:path-BWBLK802?id=BWBLK802

So I then tried the first suggestion, and the redirect doesn’t seem to have any effect, here’s a live url: https://parkingblackpool.co.uk/blackpool-hotels-with-parking/boulevard-hotel-blackpool?id=WVBLK240

I applied the following rule:

[[redirects]]
from = "/blackpool-hotels-with-parking/boulevard-hotel-blackpool?id=:id"
to = "/blackpool-hotels-with-parking/boulevard-hotel-blackpool-:id"
status = 301
query = {id = ":id"}

I also re-deployed and added force = true, but it had no effect.

One big takeaway here is that placeholders will only work between two paths, i.e. /:placeholder/, and not as just a part of the path (with the exception being the last path, where you can append a query string param.

With this in mind, is there a nomenclature which will work for you?

Hey @Scott thanks, I’m not sure I fully understand, but would slug suit?

Sorry @morganfeeney, I think I misunderstood the original request!

[[redirects]]
  from = "/blackpool-hotels-with-parking/blackpool-(beach)"
  to = "/blackpool-hotels-with-parking/blackpool-(beach)-:id"
  status = 301
  query = {id = ":id"}

That will redirect /blackpool-hotels-with-parking/blackpool-(beach)?id=demo to /blackpool-hotels-with-parking/blackpool-(beach)-demo?id=demo.

Head’s up – we automatically pass through the query string parameter from the ‘from’ path to the new (‘to’) path.

If you don’t want ?id=demo in the new (‘to’) path, you can update your rule with a alternate, dummy query string parameter… which actually comes in handy, for knowing if somebody followed the redirect or browsed directly to the new (‘to’) path.

  to = "/blackpool-hotels-with-parking/blackpool-(beach)-:id?redir"
2 Likes

Hey @Scott from what I’ve read in the docs, it’s not possible to remove the query after a redirect, therefore marking as the solution.

1 Like