Expanding functionality of redirects

A few people have questions & wishlists regarding redirects with a _redirects file. If you have an opinion you’d like to share, comment below. The more information we have about your use case, the better! :slight_smile:

1 Like

I want to pre-render different static HTML page versions for mobile and desktop in an SPA, and redirect the user to a correct one on first load to avoid screen flickering. Ideally I’d like to have a redirect rule based on screen size.

Conditional redirect based on headers would be handy. One potential use case: redirect to webp image for browsers that support it via the accepts header and fallback to png.

3 Likes

Can you redirect to a netlify function?

Hmm, this article is old, but we believe still mostly valid as general guidance:

While yes, there are cases where this behavior would make things easier, and we do have an open feature request to extend things in this way to which I’ve added this thread’s desires and weight, it’s not sure that we’d implement it considering the TL;DR for that article which is summarized near the top, in a highlight:

It’s worth re-iterating: it’s very rarely a good idea to use user agent sniffing. You can almost always find a better, more broadly compatible way to solve your problem!

You can, @devotox, redirect to a function. Or you could load a function directly that does nothing except sniff the UA and return an HTTP 301 redirect, to the proper version of your site. But it’s pretty inelegant and probably not super speedy to put a function execution in your normal request path…

To be clear: I wasn’t talking about conditional based on the UA, I was talking about conditional based on the Accepts header.

1 Like

Ah, yes, sorry! Still not possible but less of a bad idea IMNSHO :wink:

We have a separate feature request for things like that which we definitely are considering on the roadmap, so I’ve also linked this thread over there so we can write back here if things change.

1 Like

Adding to this. My blog has been running for over a decade and there’s a LOT of hotlinking to scripts I’ve written (this is pre-github/cdns etc).

Being able to conditionally redirect based on referrer (or referrer host) would be extremely useful (for controlling hotlinking).

Going by the new analytics Netlify offers, I’m pretty sure hotlinking to a script (that now 404s) is the cause of my overhead of bandwidth.

noted, I’ve added that detail to the feature request.

I think it would be very useful to be able to mark an url as accepting any query parameter and simply passing them through. For example, I imagine this in my redirects file:

/path/* *=* https://other.url/:splat

Meaning to pass through all query parameters from the original URL, and also redirecting if there are no query params. I really don’t understand why this is not the default behavior though, but it would be awesome anyways to have this feature.

2 Likes

It would be nice if we could get some basic statistics on our Redirects. I’ve got some redirects that I’m not sure if they are needed anymore, if people are still hitting the old URL or not, and having some usage statistics around how frequently a given redirect rule is hit would be nice.

I second @adrm’s feature request: passing through all existing query parameters is a must-have. I know we can list all the possible combinations of parameters but that’s not a scalable or maintainable solution when there are thousands of possible combinations.

My use-case is migrating an old WordPress site over to a modern stack: I have to implement redirects from pages with extensions like “.php” and “.htm”, but I must keep the existing query string. Query parameters are used for marketing channel attribution: it’s how we know whether a Facebook campaign makes money or how many leads an ad has generated.

Totally agree with @remy.

One of my use cases is similar: respond with the optimal pre-compressed version of a file depending on Accept-Encoding. For instance we have assets pre-compiled during build-time with maximum compression (gzip level 9 and brotli level 11):

assets/css/
  main.7bca136736.css
  main.7bca136736.css.gz
  main.7bca136736.css.br

For run-time compression I would typically use gzip level 5 and brotli level 4 for a good file size to compression time ratio. But being able to serve heavily pre-compressed files would be ideal.

So I’d love to be able to do something like this:

# netlify.toml (concept, not currently working syntax):
[[redirects]]
  from = "/assets/css/*.css"
  to = "/assets/css/:splat.css.br"
  conditions = {
    Accept-Encoding = ["br"]
  }
  [redirects.headers]
    Content-Encoding = "br"

(I know Brotli is on the Netlify roadmap, this is just one of my use cases)

thanks for that additional info @jbmoelker - we’ll definitely keep you all updated when there is movement on this :muscle: