---
title: "getRelativePath"
description: "计算从一个 URL 到另一个 URL 的相对路径，常用于动态路由生成和链接处理。"
seo_title: "getRelativePath"
seo_description: "Compute the relative path between two URLs, returning a path that navigates from the source location to the target location."
canonical_url: "https://core.mhaibaraai.cn/docs/utilities/url/get-relative-path"
---
# getRelativePath

> 计算从一个 URL 到另一个 URL 的相对路径，常用于动态路由生成和链接处理。

## 用法

`getRelativePath` 函数用于获取两个 URL 之间的相对路径。

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

getRelativePath('https://example.com/a/b', 'https://example.com/a/c')
// '../c'

getRelativePath('https://example.com/a', 'https://example.com/a/b/c')
// 'b/c'
```

## API

### `getRelativePath(from, to)`

获取两个 URL 之间的相对路径。

### 参数

**from** (`string`) *required*: 起始 URL。

**to** (`string`) *required*: 目标 URL。

### 返回值

**返回值** (`string`): 相对路径。

## Changelog

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


## Sitemap

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