---
title: "ensureTrailingSlash"
description: "确保 URL 字符串以斜杠结尾，若已有则原样返回，常用于路径标准化处理。"
seo_title: "ensureTrailingSlash"
seo_description: "Ensure a URL ends with a single trailing slash, adding one when missing so directory-style links resolve consistently."
canonical_url: "https://core.mhaibaraai.cn/docs/utilities/url/ensure-trailing-slash"
---
# ensureTrailingSlash

> 确保 URL 字符串以斜杠结尾，若已有则原样返回，常用于路径标准化处理。

## 用法

`ensureTrailingSlash` 函数用于确保 URL 以斜杠结尾。

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

ensureTrailingSlash('https://example.com') // 'https://example.com/'
ensureTrailingSlash('https://example.com/path') // 'https://example.com/path/'
ensureTrailingSlash('https://example.com/') // 'https://example.com/'
```

## API

### `ensureTrailingSlash(url)`

确保 URL 以斜杠结尾。

### 参数

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

### 返回值

**返回值** (`string`): 带尾部斜杠的 URL。

## 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.
