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:

https://docs.netlify.com/routing/redirects/redirect-options/#redirect-by-country-or-language

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.

1 Like

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: https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent) 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.

1 Like

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

2 Likes

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