Search code examples
c#.nethttp-status-code-301

301 Redirect with unicode characters - C#


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.


Solution

  • 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.