Redirect like "/paintings page=:page /paintings-page-:page 200" not working as intended

Hi,

I noticed some of my redirect rules are not working. I think I would have verified that they worked when I added them, and that they have broken since, but I’m not sure.

One example rule is

/paintings page=:page /paintings-page-:page 200

which is meant to make e.g. Paintings – 18th century blog show Paintings – 18th century blog but as you can see by visiting those URLs, the first one is not so rewritten – it just renders Paintings – 18th century blog and ignores the param.

This site is a static mirror of an old blog, and this was intended as a workaround so the pagination could keep working with the old URLs.

@henrik Did the forum software replace the question mark with a space in your example?

The rules look correct (based on the documentation) so I think potentially the issue is that your redirects are not applying at all: do you have any working redirect rules? I’d probe whether or not there’s an error with your _redirects file formatting or file placement.

:warning: Make sure we can access the file
If you’re running a build command or site generator, the _redirects file should end up in the folder you’re deploying.

As an aside, you might find it easier to use the netlify.toml configuration file because it’s clearer what exactly the rule is configured to do, and you could use the splat functionality because your website uses the page pagination in quite a few places.

[[redirects]]
  from = "/*"
  query = {page = ":page"}
  to = "/:splat-page-:page"
  status = 200

This example would rewrite:

/paintings?page=2 -> /paintings-page-2
/links?page=4 -> /links-page-4

Also, minor note about language: a 200 is a rewrite so the URL will not change – it might be easier when debugging to use a 302[1] so you can see exactly which URL you’re being sent to and then switch to a 200 once you’re happy (if you do desire rewrites instead of redirects) :slight_smile:

[1] don’t use a 301 to debug because your browser will cache it which can cause a headache: a 301 should only be used once you’re happy and willing to commit to a redirect for the rest of time.

1 Like

Hi! Back several months later, running into the same issue again with a new site :slight_smile:

@shrink Thanks so much for your suggestions! I’m afraid I still haven’t been able to make it work, though.

I now have a netlify.toml like

[[redirects]]
  from = "/index.html"
  query = {page = ":page"}
  to = "/index-page-:page.html"
  status = 302

[[redirects]]
  from = "/*"
  query = {page = ":page"}
  to = "/:splat-page-:page"
  status = 302

[[redirects]]
  from = "/debug"
  to = "/my-art-and-illustration"
  status = 302

[[redirects]]
  from = "/debug2"
  query = {page = ":page"}
  to = "/books"
  status = 302

[[headers]]
  for="/2008/10/26/new-art-platinum"
  [headers.values]
    Content-Type = "text/html; charset=utf-8"
# … a bunch of similar header sections for pages without an extension

If I visit Johanna's blog, it effectively ignores the page param. No redirect.

If I visit Travels – Johanna's blog, it’s the same.

If I visit https://blog-johannaost-com.netlify.app/debug, I do end up on My art and illustration – Johanna's blog, so that one’s working!

If I visit https://blog-johannaost-com.netlify.app/debug2 with no page param, I get a 404 as expected. If I visit https://blog-johannaost-com.netlify.app/debug2?page=123 I end up at https://blog-johannaost-com.netlify.app/debug2-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123?page=123, so there’s some kind of recursion going on. Perhaps something to do with what order rules are applied and whether only the first one applies. I didn’t see this explicitly stated in the docs.

But bottom line is I still haven’t cracked how to get this working.

Hi, @henrik. The issue you are experiencing is because your rules are not forced.

When you query this URL:

https://blog-johannaost-com.netlify.app/index.html?page=2

That page exists and because the rules isn’t forced it doesn’t apply at all. This just serves the page /index.htm with a 200 response. Your query parameter never gets considered because the rule itself never applies.

Now, for the /debug path, there is no just page for that site (no page /debug.html or /debug/index.html). This triggers the non-forced rule and redirects to /my-art-and-illustration.

For the last example, this is the URL requested:

https://blog-johannaost-com.netlify.app/debug2?page=123

This is triggering the second redirect:

[[redirects]]
  from = "/*"
  query = {page = ":page"}
  to = "/:splat-page-:page"
  status = 302

There is no page so the /* path matches - but only with a query parameter. Without the query parameter you get the expected 404. With a page parameter the rule now works as follows:

  • :splat = debug2
  • :page = 123

With a to setting of /:splat-page-:page this redirects to the following path with a 302:

/debug2-page-123?page=123

Now, that also matches the same rule so it redirects to this:

/debug2-page-123-page-123?page=123

The redirect keeps rinsing and repeating to give you the long URL you posted:

https://blog-johannaost-com.netlify.app/debug2-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123-page-123?page=123

I think if you force the rule like so it will achieve the result you are looking for:

[[redirects]]
  from = "/index.html"
  query = {page = ":page"}
  to = "/index-page-:page.html"
  status = 200!

If that doesn’t work or if there are other questions, please let us know.

Thank you, @luke!

That did it – though the syntax with the Toml file appears to be force = true. This appears to solve my problem:

[[redirects]]
  from = "/index.html"
  query = {page = ":page"}
  to = "/index-page-:page.html"
  force = true
  status = 200

[[redirects]]
  from = "/*"
  query = {page = ":page"}
  to = "/:splat-page-:page"
  force = true
  status = 200

Hi, @henrik. Yes, it is force = true not 200!. I had given the _redirects syntax not the netlify.toml syntax. Thank you for catching that and correcting it.

Also, thank you for taking the time to share the working solution. :+1: