Expanding functionality of redirects

Cookie based redirect/rewrite

In my static app I’m storing language info on localeStorage. My urls are like this:
/ -> home page with default locale
/ch -> home page with chinese locale

If user visited my app before and set locale to chinese then visits homepage “/” without “/ch” he is redirecing to “/ch” as soon as possible on client side code. Until I read localeStorage then redirect, user sees default locale. Reading locale from cookie and 302 redirect would be better.

My app also redirects visitors to their browser language path if they are at trying to access “/” and if didnt explicitly select another locale. Reading browser locale from window.navigator.language. Instead of doing that i would prefer read “accept-language” header then 302 redirect.

Hmm, if you use a 301 redirect, that should happen automatically, if you use language based redirects:

Does that not work well for you? Shouldn’t require anything on the client side, should never show the wrong content…

Wow. I was didn’t expect to this is exist. This is makes possible my use case I mentioned above. This is awesome.

So can I redirect to user different path if its language anything else than a certain language. Something like:

// I add a exclamation mark for example.
// Imagine my website language is chinese and I wanna redirect users to english if their language is not chinese.
/  /en  302  Language=!zh

Also I’m ignoring bots (e.g googlebot) in my client side redirect code for let the crawlers index pages on its correspondig language. Is this redirect rules do that for me? I dont want to crawlers to be redirected from / to /en because of its browser language.

I’d love this to work:
ext:php https://somemirrorofsitewithphpexecution.com/:splat 200!

Thanks for the suggestion @niansa!

1 Like

We would love to be able to redirect Internet Explorer users. So User-Agent based redirection would be a great addition to us.

We no longer support Internet Explorer on our platform and are now showing users a page informing them of this fact. Our current redirection is based on a Vue.js route middleware, but even maintaining support for a “we do not support IE” page has proved to be unnecessarily difficult.

howdy @joaobarcia and welcome to our community!

We think that is a bit of an antipattern (see this article for the reasoning: Browser detection using the user agent - HTTP | MDN) so we don’t support it now, but if we change our mind and implement it, we’ll post in this thread to let everyone know :slight_smile:

1 Like
  1. Unfortunately, we don’t support negative matches like that. You’ll need a set of content for the language you want, and to make a 404 page for the content you don’t, something like this may work:
  /* /no-content-for-you.html 302! Language=zh
  /* /en 302!
  1. No rules to ignore bots provided on our service. You can set things like robots.txt or an X-Robots-Tag custom HTTP response header to control that

Thanks for the reply and the update :slight_smile:

I’d like to redirect based on the presence of a cookie.

I’m using firebase auth and once a user logs in they have a specific cookie present.

For all users with this cookie present I’d like to redirect them from the root site.com to the subpath where the logged in application is hosted site.com/app. Obviously I can do this client side but it makes for a horrible experience when done like this.

I can’t tell if this is possible using Netlify’s redirect rules.

2 Likes

Yup, client side will be your best bet for “straightforward implementation” at present; we don’t have any functionality like that (yet) but we are working to enable it with some active work.

If you could have a special URL (site.com/amiloggedin) that proxy’d to a function, it could detect the cookie and return conditional redirects, but that isn’t quite as simple as setting up a single redirect.

2 Likes

Interesting idea, I’ll look into that. Also if you wouldn’t mind updating this thread when the active work is completed that would be awesome!

2 Likes

we definitely will, @goleary!

2 Likes

I’m also interested in cookie-based redirects and rewrites (rewrites, in particular). That would help us avoid the implementation of the extra lambda function and keep everything clean with static rendering on Nextjs

4 Likes

yes definitely. We will absolutely post an update here once we have something concrete.

I’d like to +1 the request for supporting conditions based on headers.

My use case:
I’m splitting an existing site into two and would like anything that returns a 404 on the primary URL to redirect to the secondary URL. I can do that in my application but it would be nice if I didn’t have to mess with the routing of my app and had a simple rule in the redirect functionality.

eg:

[[redirects]]
  from = "*"
  to = "http://example.com/:splat"
  status = 301
  conditions = {Headers = { "Status Code": "404" }}
  headers = {X-From = "Netlify"}

I would love to be able to use regular expressions or the like in redirects.

I have a bunch of URLs that originally had underscores in them, but now have dashes instead (something something SEO). Unfortunately, they are still linked to from around the net using the underscore URLs and I would like to redirect them to their proper dasherized versions instead of serving 404 pages.

Something like

from = "\/(.+)_(.+)$"
to = "/$1-$2"

could probably solve my use case, especially if the next request is then processed following the same rule.

Even better would be some way equivalent of modifying the path with Javascript, ie

"/foo_bar_baz".replaceAll(/_/g, "-");

Not sure how that would look in config, perhaps something like

from = "/blog/*"
replace = {pattern = /_/g, with = "-"}

Or perhaps to redirect a daily archive URL (ie /posts/2020/01/20) to the yearly archive:

from = "/posts/*"
replace = {pattern = /(\d+)\/(\d+)\/(\d+)/g, with = "$1"}

Not sure what’s possible im TOML :man_shrugging:

1 Like

Do you have other wildcard redirects in your primary URL/app? Otherwise, thanks to Netlify’s shadowing behavior, you should be able to just add this redirect:

[[redirects]]
  from = "*"
  to = "http://example.com/:splat"
  status = 301
  headers = {X-From = "Netlify"}

Any request for a page that exists (e.g., /about.html) will correctly serve the existing page. Any request for a page that doesn’t exist (e.g., /do-not-exist.html) will be forwarded to example.com. But if you have existing wildcard redirects (such as a React Single-Page-Application), this might conflict…

I think the edge handlers will be perfect for this sort of thing: https://www.netlify.com/products/edge/edge-handlers — but it’s still in early access.

I guess a workaround for the time being could be to use a Netlify Function to “rewrite” the URL and redirect all unmatched requests for /blog/* to the Netlify Function (/.netlify/functions/rewriteUrl). Happy to share more, if that’d be useful.