Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalizing URI to make it work correctly with MakeRelativeUri

Dim x AS New URI("http://www.example.com/test//test.asp")
Dim rel AS New URI("http://www.example.com/xxx/xxx.asp")
Console.Writeline(x.MakeRelativeUri(rel).Tostring())

In here output is:

../../xxx/xxx.asp

Which looks correct almost all web servers will process the two of the following as same request:

http://www.example.com/test//test.asp
http://www.example.com/test/test.asp

What's the best way to fix this behaviour is there any API to do this, or shall manually create a new URI and remove all // in the path?

like image 237
dr. evil Avatar asked Dec 09 '09 15:12

dr. evil


People also ask

What does normalize URL do?

URL normalization (also called URL canonicalization) is the process by which URLs are modified and standardized in a consistent manner. The purpose of URL normalization is to transform a URL into a normalized or canonical URL so it is possible to determine if two syntactically different URLs are equivalent.

What does it mean to normalize a path?

Normalizing a path involves modifying the string that identifies a path or file so that it conforms to a valid path on the target operating system. Normalization typically involves: Canonicalizing component and directory separators. Applying the current directory to a relative path.


1 Answers

It seems that replacing all multi-slashes with one slash in LocalPath portion is the best solution. IIS does so even when passing URL to ASP.net.

Following code will do the trick.

x = new Uri(string.Format("{0}://{1}:{2}{3}{4}", x.Scheme, x.Host, x.Port, Regex.Replace(x.LocalPath, @"(?<!\:)/{2,}", "/"), x.Query));
like image 178
dereli Avatar answered Nov 15 '22 05:11

dereli