Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting exact domain name from any URL [duplicate]

Tags:

I need to extract the exact domain name from any Url.

For example,

Url : http://www.google.com --> Domain : google.com

Url : http://www.google.co.uk/path1/path2 --> Domain : google.co.uk

How can this is possible in c# ? Is there a complete TLD list or a parser for that task ?

like image 717
Yazginin Firati Avatar asked May 12 '11 20:05

Yazginin Firati


People also ask

Can URL be duplicated?

This means that the URL in question is technically identical to at least one other indexable URL. This could be URLs that are only different based on case, or have the same query string parameters and values (but in a different order).

What happens if two websites have the same content?

That “one place” is defined as a location with a unique website address (URL) - so, if the same content appears at more than one web address, you've got duplicate content. While not technically a penalty, duplicate content can still sometimes impact search engine rankings.


1 Answers

You can use the Uri Class to access all components of an URI:

var uri = new Uri("http://www.google.co.uk/path1/path2");  var host = uri.Host;  // host == "www.google.co.uk" 

However, there is no built-in way to strip the sub-domain "www" off "www.google.co.uk". You need to implement your own logic, e.g.

var parts = host.ToLowerInvariant().Split('.');  if (parts.Length >= 3 &&     parts[parts.Length - 1] == "uk" &&     parts[parts.Length - 2] == "co") {     var result = parts[parts.Length - 3] + ".co.uk";      // result == "google.co.uk" } 
like image 83
dtb Avatar answered Nov 02 '22 14:11

dtb