Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove additional slashes from URL

In ASP.Net it is posible to get same content from almost equal pages by URLs like localhost:9000/Index.aspx and localhost:9000//Index.aspx or even localhost:9000///Index.aspx

But it isn't looks good for me.

How can i remove this additional slashes before user go to some page and in what place?

like image 509
demo Avatar asked May 27 '15 13:05

demo


People also ask

Why do URLs have two slashes?

URLs with a double slash in the path can be generated by content management systems, plugins or broken HTML, and are often caused by issues with relative linking and/or the base URL.

What are the slashes in a URL?

The addition of a slash at the end of a URL instructs the web server to search for a directory. This speeds the web page loading because the server will retrieve the content of the web page without wasting time searching for the file.

When should I use a trailing slash in my URL?

If your site has a directory structure, it's more conventional to use a trailing slash with your directory URLs (for example, example.com/directory/ rather than example.com/directory ), but you can choose whichever you like. Be consistent with the preferred version. Use it in your internal links.


2 Answers

Use this :

url  = Regex.Replace(url  , @"/+", @"/");

it will support n times

enter image description here

like image 199
Royi Namir Avatar answered Nov 15 '22 00:11

Royi Namir


see

https://stackoverflow.com/a/19689870

https://msdn.microsoft.com/en-us/library/9hst1w91.aspx#Examples

You need to specify a base Uri and a relative path to get the canonized behavior.

Uri baseUri = new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri, "catalog/shownew.htm");
Console.WriteLine(myUri.ToString());
like image 28
Bernhard Avatar answered Nov 14 '22 22:11

Bernhard