How to effectively trim URLs without triggering infinite redirects


I’ve been trying to figure this out but I haven’t been able to. The case scenario is fairly simple:
I have url’s that have more information than needed for SEO purposes. That is, the URL follows a pattern like this:
What’s needed is to trim the seo description through redirects,
/level1/level2/seo_description -> /level1/level2/
and the attempts have been the following redirect files:

# Option 1
/level1/level2/* /level1/level2/ 301
/* /index.html 200

# Option 2
/level1/level2/* /level1/level2 301
/* /index.html 200

Sorry for the lack of creativeness, but yes: the only difference is the tailing slash. That’s when I realized that railing slashes are always added before processing redirects.
This triggers an infinite redirect loop, as it goes constantly into the same redirects (and it makes sense)
So, how can I achieve the needed behavior described above?

Any help is much appreciated!

Hi to achieve the desired behaviour of trimming the SEO description from your URLs, you can use Netlify’s redirect rules. These rules can be specified in a _redirects file or in your netlify.toml configuration file.

Here’s an example of how you can set up a redirect rule in your netlify.toml file:

  from = "/level1/level2/*"
  to = "/level1/level2/"
  status = 301
  force = true

In this example, any URL that matches the pattern /level1/level2/* will be redirected to /level1/level2/. The * is a wildcard that matches any characters. The status = 301 line sets the HTTP status code for the redirect to 301 (Moved Permanently), and force = true means that this redirect will take place even if there is existing content at the “from” path.

Also if you have a large number of redirects (10,000 or more), it’s recommended to use wildcards or placeholders as much as possible. For more complex redirect setups, Edge Functions would be a better option.

Hi @SamO ! Thank you very much for your reply. How is this any different from the behavior I have already tried? I did do it using the _redirects file, with syntax equivalent to the one you are adding in the netlify.toml file. But this is what triggers the infinite loop. Is it the force = true what makes a difference?

Yes. Force makes the difference. You could also use it as:

/level1/level2/* /level1/level2/ 301!

Hi I am experiencing exactly the same issue. Force has made zero difference.
Netlify still reports a 404

I know the page exists because if I visit it from a link in my nextjs application it resolves as expected.

Hi, @ecooffset. We can troubleshoot here if you share the redirect rules you created and let us know which URLs are not redirecting as desired.

Also, you are on a paid team type so if you open a support ticket with us, we can troubleshoot this privately so you don’t need to share the URLs publicly here.

You should be able to do this with:

  from = "/level1/level2/:path+"
  to = "/level1/level2/"
  status = 301
  force = true

:path+ Will only match routes with 1 or more characters while using * also matches empty routes and create a redirect loop.