ThisCalculator

Everyday

Get Domain Name from URL: How to Extract It

Updated · Standard formula, shown below

This article explains how to extract a domain name from a URL, covering both manual methods and automated tools. You’ll learn what a domain name is, how to parse URLs correctly, and common pitfalls to avoid.

What Is a Domain Name?

A domain name is the human-readable address of a website, such as example.com or en.wikipedia.org. It translates to the IP address that computers use to locate servers. In a URL like https://www.example.com/path?query=1, the domain name is example.com (or sometimes www.example.com, depending on the context). The domain name is part of the larger URL, which includes the protocol (https://), path (/path), and query string (?query=1).

Why Extract the Domain Name?

Extracting a domain name is useful for several reasons:

  • Data analysis: Grouping web traffic by domain.
  • Security: Identifying the source of phishing links.
  • SEO: Analyzing backlinks or competitors.
  • Content filtering: Blocking or allowing specific sites.
  • Bookmarking: Saving a clean, readable address.

Manual Extraction: Step-by-Step

For a single URL, you can manually identify the domain by looking at the part after the protocol (e.g., https://) and before the first slash. For example, in https://sub.example.co.uk/page, the domain is example.co.uk (the registrable domain), but the hostname is sub.example.co.uk. If you need the registrable domain (the part you’d register), you must account for public suffixes like .co.uk or .com.au. The Public Suffix List is the standard reference for this.

Using JavaScript to Extract the Domain

In web development, you often need to get the domain from a URL string. The simplest way is to use the built-in URL object:

const url = new URL('https://www.example.com/path');
console.log(url.hostname); // "www.example.com"

This gives the hostname, which includes subdomains. To get the registrable domain (without www or other subdomains), you can use a library like psl (Public Suffix List) or manually strip common prefixes. For example:

const psl = require('psl');
const domain = psl.get(url.hostname); // "example.com"

In a browser, you can also use window.location.hostname to get the current page’s hostname.

Using Regular Expressions (Regex)

For simple cases, a regex can extract the domain from a URL string. A common pattern is:

/^(https?:\/\/)?([^\/]+)/i

This matches the protocol (optional) and then captures everything up to the first slash. However, regex is fragile for complex URLs with unusual ports or authentication. For example, http://user:pass@example.com:8080/path would capture user:pass@example.com:8080, which is not the domain. A better approach is to use a URL parser library or the built-in URL object.

Common Mistakes and Pitfalls

  • Including the protocol (https://) in the domain.
  • Including the path or query string.
  • Forgetting about ports (example.com:8080).
  • Assuming www is always part of the domain.
  • Not handling internationalized domain names (IDN) like münchen.de.
  • Overlooking public suffixes (e.g., .co.uk is not a domain, but example.co.uk is).

Tools and Libraries for Bulk Extraction

If you need to extract domains from many URLs, manual parsing is impractical. Several libraries and online tools can help:

  • Python: tldextract library accurately separates subdomain, domain, and suffix.
  • Node.js: psl (Public Suffix List) package.
  • Online tools: Many free websites allow you to paste a list of URLs and get domains.
  • Spreadsheet formulas: In Excel or Google Sheets, you can use =REGEXEXTRACT (Google Sheets) or a combination of FIND and MID to extract the domain.

Frequently Asked Questions

What is the difference between a domain name and a URL?

A URL (Uniform Resource Locator) is the full address, including protocol, domain, path, and query. The domain name is just the part that identifies the website, like example.com.

How do I get the domain name from a URL in Excel?

You can use a formula like =MID(A1, FIND("//", A1)+2, FIND("/", A1, FIND("//", A1)+2) - FIND("//", A1) - 2) to extract the hostname. For the registrable domain, you’d need a more complex formula or a custom function.

Does the domain name include “www”?

Technically, www is a subdomain, not part of the registrable domain. So www.example.com is a hostname, while example.com is the domain. In practice, many people refer to the full hostname as the domain.

What is a public suffix?

A public suffix is a domain suffix under which you can register domains, such as .com, .co.uk, or .github.io. The Public Suffix List maintains these to help determine the registrable domain.

Can I extract the domain from a URL without a library?

Yes, for simple URLs you can use string manipulation or regex. But for edge cases (ports, auth, IDN), a library is more reliable.

Why do I get a different result for “example.com” vs “www.example.com”?

Because they are different hostnames. The registrable domain is the same, but the hostname includes the subdomain. Your extraction method determines which one you get.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.