Darksorrow
Darksorrow

Reputation: 407

Nodejs redirect url encoding

I have a server that makes url redirection using nodejs. I use this to make the redirection : response.writeHead(302, {Location: url}); response.end();

This works well with normal url like google.com but when I have other characters like cyrillic it bugs for example if I do a url = 'ru.wikipedia.org/wiki/Путин,_Владимир_Владимирович' (with https:// infront) then the redirection bugs. Do I have to somehow reencode the string before passing it to the redirection? because when I make a console.log(url), it's displaying the correct url with the cyrilic letters.

After some more test i manage to see that the data encrypted is as follow using node-icu-charset-detector: ----[NOTICE] charset: ISO-8859-2 ----[NOTICE] redirect: https://ru.wikipedia.org/wiki/Путин,_Владимир_Владимирович

And the link I'm getting on my browser is like 'https://ru.wikipedia.org/wiki/%1FCB8=,%12;048%3C8@%12;048%3C8@%3E28G'

Upvotes: 1

Views: 1924

Answers (1)

hassansin
hassansin

Reputation: 17498

You can encode the url since HTTP header values doesn't support utf-8 encoded value:

response.writeHead(302, {Location: encodeURI(url)});

Upvotes: 1

Related Questions