2

I need to do a 301 redirect on a URL that may have Unicode characters in it.

HttpUtility.UrlEncode isn't doing what I need because if I encode the whole URL it encodes any ':' or '/'

HttpUtility.UrlEncode("http://www.हिन्दी.com") = http%3a%2f%2fwww.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com

(also: http://www.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com doesn't seem to work in firefox or IE, but it does in Chrome)

Only other thing I can think of is to encode the different parts of the URL so that the protocol doesn't get encoded.

topher-j
  • 2,221
  • 5
  • 24
  • 35
  • I think you'd need to encode the domain into an [IRI](http://www.w3.org/International/articles/idn-and-iri/) / punycode (yuck name) before passing it to the redirect rather than just URL encoding what you've got. – Rup Feb 16 '11 at 11:12

2 Answers2

3

You need to take a look at RFC 3490 which details how to correctly encode international domain names -- this is also why when you encode just the domain portion it only works in Chrome)

Community
  • 1
  • 1
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
1

So I figured out a almost 100% solution to this. Thanks to Rowland Shaw and Rup for pointing me in the direction of IDNs.

I tried using an IdnMapper, whose function GetAscii will convert unicode domain names to punycode, but I didn't have the domain separated from the rest of the URL. I tried putting the url into a Uri object, but I would get a UriFormatException if the url had unicode characters.

That led me to: http://msdn.microsoft.com/en-us/library/system.uri(v=VS.90).aspx

which tells how to enable the Uri class to accept unicode and do the IDN and IRI conversions. It says you have to add something to the .NET 2.0 machine.config file, but you can put the line in web.config and it will work.

After I got the Uri working with unicode, I pieced together the url and did a redirect:

Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", uri.Scheme + "://" + uri.DnsSafeHost + uri.PathAndQuery + uri.Fragment);
Response.End();

This works for Chrome and Firefox 3.6, but fails in IE8. I'm still trying to solve that problem and will update here if I find a solution.

topher-j
  • 2,221
  • 5
  • 24
  • 35
  • The IE8 error only appears when the domain of my redirecting URL was local. So http://localhost/redirectURL which redirects to a IRI would fail, but http://www.example.com/redirectURL which redirects to the same IRI would work. Odd. – topher-j Feb 18 '11 at 18:01