Extract the Domain From a URL in Excel
By ExcelReady TeamUpdated 7 min read
The short answer
To extract the domain from a URL in Excel, take the text after the "//" of https:// and before the next "/", which starts the page path. In Microsoft 365 or Excel 2024, use =TEXTBEFORE(TEXTAFTER(A2,"//")&"/","/"). In any version, use =MID(A2,FIND("//",A2)+2,FIND("/",A2&"/",FIND("//",A2)+2)-FIND("//",A2)-2). Wrap either one in SUBSTITUTE(...,"www.","") to drop the www part.
Key takeaways
- A URL's domain sits between the "//" after the protocol and the first "/" of the path.
- Adding "/" to the end of the URL (A2&"/") stops errors on URLs that have no path.
- SUBSTITUTE(...,"www.","") removes www; TEXTBEFORE with several delimiters also strips ports and query strings.
- TEXTAFTER(host,".",-2) gives the last two parts of a domain, but not for endings like co.uk.
On this page+
To extract the domain from a URL in Excel, take the text after the // of the protocol and before the next /. In Microsoft 365 that is =TEXTBEFORE(TEXTAFTER(A2,"//")&"/","/"); in any version, MID and FIND do the same job. This guide is for anyone cleaning a list of web links, from marketing reports to backlink audits, and it shows how to drop www, ports and query strings too.
How a URL splits into parts#
A URL has a fixed order of parts, and the domain is always the piece right after //:
| Part | Example | Ends at |
|---|---|---|
| Protocol | https:// | The // |
| Host (domain) | www.contoso.com | The next /, ?, # or : |
| Port (rare) | :8080 | The next / |
| Path | /blog/post | The ? |
| Query string | ?id=3 | The end |
So the job is: find //, then find the first / after it, and take what's in between. That makes URLs trickier than email addresses, which have exactly one @. If you need the domain from an email instead, see how to extract the domain from an email address.
All the examples use this list in A2:A6:
| A | Domain wanted | |
|---|---|---|
| 2 | https://www.contoso.com/blog/post?id=3 | contoso.com |
| 3 | http://shop.fabrikam.net/cart | shop.fabrikam.net |
| 4 | https://contoso.co.uk:8080/login | contoso.co.uk |
| 5 | www.northwindtraders.com/about | northwindtraders.com |
| 6 | https://tailspintoys.com?ref=email | tailspintoys.com |
Extract the domain from a URL with TEXTAFTER and TEXTBEFORE#
In Microsoft 365 or Excel 2024, two nested functions do it. TEXTAFTER takes everything after //, and TEXTBEFORE keeps everything before the next /:
=TEXTBEFORE(TEXTAFTER(A2,"//")&"/","/")For A2 this returns www.contoso.com. The &"/" adds a slash to the end, so a URL with no path, like https://tailspintoys.com, still has a / to stop at. Without it, TEXTBEFORE would return #N/A.
The guide to TEXTBEFORE and TEXTAFTER explains both functions, and you can practise them in the TEXTBEFORE and TEXTAFTER lesson.
Handle URLs without https://#
Row 5 has no protocol, so TEXTAFTER can't find // and returns #N/A. Its sixth argument, if_not_found, fixes that. Tell it to return the whole URL instead:
=TEXTBEFORE(TEXTAFTER(A2,"//",,,,A2)&"/","/")The four commas skip the optional arguments in between. Now row 5 returns www.northwindtraders.com.
Strip ports and query strings too#
TEXTBEFORE accepts a list of delimiters in curly braces and stops at whichever comes first. Add ?, # and : to the list:
=TEXTBEFORE(TEXTAFTER(A2,"//",,,,A2)&"/",{"/","?","#",":"})Row 4 now returns contoso.co.uk instead of contoso.co.uk:8080, and row 6 returns tailspintoys.com instead of tailspintoys.com?ref=email.
Extract the domain with MID and FIND (any version)#
In Excel 2021 or older, use MID to cut out the text between two positions, and FIND to locate them. It's easiest to understand in three helper cells:
| Cell | Formula | Result for A2 | What it finds |
|---|---|---|---|
| C2 | =FIND("//",A2)+2 | 9 | First character of the domain |
| D2 | =FIND("/",A2&"/",C2) | 24 | The next /, searching from C2 |
| E2 | =MID(A2,C2,D2-C2) | www.contoso.com | The text in between |
FIND's third argument tells it where to start searching, so it skips the two slashes in https://. Put it all in one cell by replacing C2 and D2 with their formulas:
=MID(A2,FIND("//",A2)+2,FIND("/",A2&"/",FIND("//",A2)+2)-FIND("//",A2)-2)For URLs that might lack a protocol, use IFERROR(FIND("//",A2)+2,1) as the start, so the domain is taken from the first character. The FIND function guide and the MID function guide show both syntaxes, and the article on FIND vs SEARCH explains how the two differ (FIND is case-sensitive and has no wildcards). You can also practise FIND and SEARCH in a live sheet.
Remove www from the domain#
Wrap either formula in SUBSTITUTE to delete www.:
=SUBSTITUTE(TEXTBEFORE(TEXTAFTER(A2,"//",,,,A2)&"/","/"),"www.","")Row 2 now gives contoso.com. SUBSTITUTE replaces every www. it finds, which is fine for real host names. If you want to remove it only from the start, test the first four characters instead: =IF(LEFT(B2,4)="www.",MID(B2,5,LEN(B2)),B2), where B2 holds the host. See the SUBSTITUTE function guide for more.
Get the root domain without subdomains#
Sometimes you want fabrikam.net, not shop.fabrikam.net. Take the last two dot-separated parts with a negative instance number, which counts from the end:
=TEXTAFTER(B2,".",-2,,,B2)Here B2 holds the host. shop.fabrikam.net becomes fabrikam.net, and the fallback returns contoso.com unchanged when it has only one dot. Be careful with two-part endings: contoso.co.uk would become co.uk. Excel has no list of these endings, so check them by hand or with a small lookup table.
Get the page path instead#
The same pieces give you the rest of the URL. Everything after the domain's first / is the path:
=TEXTAFTER(TEXTAFTER(A2,"//",,,,A2),"/",,,,"")Row 2 returns blog/post?id=3, and a URL with no path returns an empty cell instead of an error. To cut off the query string as well, see how to remove text after a character. For more patterns like this, the guide to extracting text between two characters is a good next read.
Count links by domain#
Once each URL has a clean domain in column B, counting is simple:
=COUNTIF(B2:B500,"contoso.com")For a full summary in Microsoft 365, list each domain once with =UNIQUE(B2:B500) in D2, then count each one in E2 with =COUNTIF(B2:B500,D2#). Always clean the domains first: Contoso.com and contoso.com count together because COUNTIF ignores case, but www.contoso.com does not, which is why removing www. matters.
Why your domain formula returns an error#
Each error points to a specific problem in the URL or the formula:
| What you see | Likely cause | Fix |
|---|---|---|
| #VALUE! from the MID version | The URL has no // | Use IFERROR(FIND("//",A2)+2,1) as the start |
| #N/A from TEXTAFTER | The URL has no // | Add the if_not_found argument: TEXTAFTER(A2,"//",,,,A2) |
| #N/A from TEXTBEFORE | The URL has no / after the domain | Add &"/" before TEXTBEFORE looks for it |
| #NAME? | TEXTBEFORE or TEXTAFTER in Excel 2021 or older | Use the MID and FIND version |
| Domain still has spaces | Spaces were copied with the URL | Wrap A2 in TRIM first |
Blank cells return errors too. Wrap the finished formula in IFERROR(...,"") so empty rows stay empty, but only after you've checked the real URLs work, because IFERROR also hides genuine mistakes.
Practise it#
Text extraction is a common task in data and marketing tests. Practise TEXTBEFORE and TEXTAFTER in a live spreadsheet: you split real text at a delimiter and each answer is checked instantly. Then practise FIND and SEARCH to build the MID version that works in every Excel, and try the exercise on this page.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
Get the bare domain from the URL in A2. In B2, use MID and FIND to take the text between "//" and the next "/" (search A2&"/"), then wrap it in SUBSTITUTE to remove "www.".
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Domain | |||
| 2 | https://www.contoso.com/blog/post?id=3 | |||
| 3 | http://shop.fabrikam.net/cart | |||
| 4 | https://tailspintoys.com/ | |||
| 5 | ||||
| 6 | ||||
| 7 |
Tip: Start at FIND("//",A2)+2. The length is FIND("/",A2&"/",start) minus start. Then SUBSTITUTE(…,"www.","").
Practise this in a lesson
Frequently asked questions
How do I get the domain name from a URL in Excel?
In Microsoft 365 or Excel 2024, use =TEXTBEFORE(TEXTAFTER(A2,"//")&"/","/"). In older versions, use MID with FIND to take the text between "//" and the next "/".
How do I remove https:// and www from a URL in Excel?
Take the text after "//" with TEXTAFTER or MID, then remove www. with SUBSTITUTE: =SUBSTITUTE(TEXTAFTER(A2,"//"),"www.",""). Add TEXTBEFORE to also cut the page path.
How do I extract the domain when some URLs have no https://?
Give TEXTAFTER a fallback with its sixth argument: TEXTAFTER(A2,"//",,,,A2) returns the whole URL when there is no "//". In older Excel, use IFERROR(FIND("//",A2)+2,1) as the start position.
How do I get the root domain without the subdomain?
Use =TEXTAFTER(B2,".",-2,,,B2) on the host name. It returns the last two parts, so shop.fabrikam.net becomes fabrikam.net. It gives the wrong answer for two-part endings like co.uk, so check those by hand.