Handling SSR pages in _redirects

Good day.

This is a general question, not so much about my site in particular:
How does one handle redirects in _redirects for non-SSG pages? That is to say, imagine I want to hit an SSR page /something, but I have a rule /s* / 404

Can I put a rule in the _redirects file that will tell Netlify that, although there’s no file at that location, keep going, and it’ll get rendered?

I.e., I want to tell it to ignore the catch-all rule for that particular path, but right now, it seems to see that there’s no generated file at that path, and so it fires that catch-all and redirects.

Thanks!

Welcome to the forums @crustyratfink

If you wanted to SSR everything (as SvelteKit) using the _redirects file

/*    /.netlify/functions/render    200

This would catch any path not existing at an actual page (e.g. /some/path/and/file.html)

If you have a fallback

/*    /404    404

you can still have other rules, but they must go above it

/link    /somewhere/else    302

If you wanted to render blog pages for instance while having the previous rules in place

/blog/*    /.netlify/functions/render-blog    200
/link    /somewhere/else    302
/*    /404    404

Is this what you are wishing to accomplish?

2 Likes

Wow, @coelmay! That is such a great answer and I thank you for it. :thank_you:

If there are any questions about that response,@crustyratfink, please reply here anytime and we’ll do our best to answer.

Hey all,

Thanks for the suggestion… I’m not sure that I’m describing the situation clearly. Or maybe I’m misunderstanding something.

Specifically: I have a page in a Next.js site: /pages/thispage.tsx, let’s say.

That page has a getServerSideProps and so isn’t statically generated.

Now, if I have a rule in my _redirects like /t* / 301… it’ll fire when I request that file because it’s not SSG, and therefore “not there”. It’ll then get caught by the wildcard.

The solution that you’re proposing if I understand it is to use a rewrite, but… how would I do that in that case? It’s not a function. Seems like /thispage /thispage 200 would loop.

So, I’d like to just say that or something like (this is made up) /something 200 and have _redirects know that it’ll be there and it should just step off and let nextjs handle it.

Make sense?

This won’t work as wildcard pattern matching for any file beginning with t is not possible.

Yes, this would loop.

If you are using Next.js and not using next export to generate static pages, you will (need to) use the Essential Next.js Build Plugin which handles ISR and SSR which has a guide on how rewrites and redirects are handled.

1 Like

For posterity, a couple of points:

  • It does seem entirely possible to forward /t* /somethingotherthing 301 but that will, indeed, catch my path and redirect it. Not what I wanted.
  • The answer is that you simply cannot redirect it that way. The _redirect stuff happens first. It just doesn’t work like that. So, you have to work around it. You could, in fact, set up a bunch of /ta*, /tb*… redirects and not /th*, if you REALLY needed to have those catch-alls.

I ended up doing something different, which just rendered this point moot, as they say, but I thought I’d follow up just to clarify that as far as I can tell, yes, you can use patterns, and either your thing is there, rendered, or _redirects is going to consider it fair game and redirect it if you have a pattern that matches.

Contrary to my previous statement, /t* /somethingotherthing 301 will work as you point out, but as you also point out, it will catch everything that starts with a t and for most uses it not ideal.

Definitely not ideal. :slight_smile: