Obtains the root domain of the current domain name.

Source: Internet
Author: User

Obtains the root domain of the current domain name.

1 /// <summary> 2 /// obtain the root domain of the current domain name 3 /// </summary> 4 /// <param name = "url"> domain name address </param> 5 // <returns> </returns> 6 public static string RootDomain (string url) 7 {8 if (! Url. StartsWith ("http ://")&&! Url. startsWith ("https: //") 9 {10 url = "http: //" + url; 11} 12 13 var uri = new Uri (url ); 14 string rootDomain; 15 switch (uri. hostNameType) 16 {17 case UriHostNameType. dns: 18 {19 if (uri. isLoopback) 20 {21 rootDomain = uri. host; 22} 23 else24 {25 string host = uri. host; 26 var hosts = host. split ('. '); 27 rootDomain = hosts. length = 2? Host: string. format ("{0 }. {1} ", hosts [1], hosts [2]); 28} 29} 30 break; 31 default: 32 rootDomain = uri. host; 33 break; 34} 35 return rootDomain; 36}

When searching for such problems on the internet, regular expressions are generally used, and several domain name suffixes are fixed, for example. cn ,. com ,. net, so I wrote this method according to my work needs.
For reference.


How to obtain the root domain name in java

String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

How to obtain the root domain name of the current page using Firefox js script

Hello! I'm glad to answer your questions!

Try this:
<Script language = "javascript">
// Obtain the Domain Name
Host = window. location. host;
Host2 = document. domain;

// Obtain the complete address of the page
Url = window. location. href;

Document. write ("<br> host =" + host)
Document. write ("<br> host2 =" + host2)
Document. write ("<br> url =" + url)
</Script>
You can learn more in the Firefox community. I hope my answers will be helpful to you. If you have any questions, please continue to consult on this platform.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.