---
title: "getRootDomain"
description: "获取 URL 的根域名(顶级域名 + 二级域名)"
seo_title: "getRootDomain"
seo_description: "Extract the root domain from a URL, correctly handling compound top-level domains so subdomains collapse to their registrable base."
canonical_url: "https://core.mhaibaraai.cn/docs/utilities/url/get-root-domain"
---
# getRootDomain

> 获取 URL 的根域名(顶级域名 + 二级域名)

## 用法

`getRootDomain` 函数用于获取 URL 的根域名(顶级域名 + 二级域名)。

```ts
import { getRootDomain } from '@movk/core'

getRootDomain('https://sub.example.com') // 'example.com'
getRootDomain('https://a.b.example.co.uk') // 'example.co.uk'
```

## API

### `getRootDomain(url)`

获取 URL 的根域名(顶级域名 + 二级域名)。

### 参数

**url** (`string`) *required*: URL 字符串。

### 返回值

**返回值** (`string`): 根域名,解析失败返回空字符串。

## Changelog

See commit history for [src/utilities/url/parse.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/utilities/url/parse.ts).


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
