| 1 |
--- |
| 2 |
title: urls.RelURL |
| 3 |
description: Returns a relative URL. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [relURL] |
| 9 |
returnType: string |
| 10 |
signatures: [urls.RelURL INPUT] |
| 11 |
aliases: [/functions/relurl] |
| 12 |
--- |
| 13 |
|
| 14 |
With multilingual configurations, use the [`urls.RelLangURL`][] function instead. The URL returned by this function depends on: |
| 15 |
|
| 16 |
- Whether the input begins with a slash (`/`) |
| 17 |
- The `baseURL` in your project configuration |
| 18 |
|
| 19 |
## Input does not begin with a slash |
| 20 |
|
| 21 |
If the input does not begin with a slash, the resulting URL will be relative to the `baseURL` in your project configuration. |
| 22 |
|
| 23 |
With `baseURL = https://example.org/` |
| 24 |
|
| 25 |
```go-html-template |
| 26 |
{{ relURL "" }} → / |
| 27 |
{{ relURL "articles" }} → /articles |
| 28 |
{{ relURL "style.css" }} → /style.css |
| 29 |
{{ relURL "https://example.org" }} → https://example.org |
| 30 |
{{ relURL "https://example.org/" }} → / |
| 31 |
{{ relURL "https://www.example.org" }} → https://www.example.org |
| 32 |
{{ relURL "https://www.example.org/" }} → https://www.example.org/ |
| 33 |
``` |
| 34 |
|
| 35 |
With `baseURL = https://example.org/docs/` |
| 36 |
|
| 37 |
```go-html-template |
| 38 |
{{ relURL "" }} → /docs/ |
| 39 |
{{ relURL "articles" }} → /docs/articles |
| 40 |
{{ relURL "style.css" }} → /docs/style.css |
| 41 |
{{ relURL "https://example.org" }} → https://example.org |
| 42 |
{{ relURL "https://example.org/" }} → https://example.org/ |
| 43 |
{{ relURL "https://example.org/docs" }} → https://example.org/docs |
| 44 |
{{ relURL "https://example.org/docs/" }} → /docs |
| 45 |
{{ relURL "https://www.example.org" }} → https://www.example.org |
| 46 |
{{ relURL "https://www.example.org/" }} → https://www.example.org/ |
| 47 |
``` |
| 48 |
|
| 49 |
## Input begins with a slash |
| 50 |
|
| 51 |
If the input begins with a slash, the resulting URL will be relative to the protocol+host of the `baseURL` in your project configuration. |
| 52 |
|
| 53 |
With `baseURL = https://example.org/` |
| 54 |
|
| 55 |
```go-html-template |
| 56 |
{{ relURL "/" }} → / |
| 57 |
{{ relURL "/articles" }} → /articles |
| 58 |
{{ relURL "/style.css" }} → /style.css |
| 59 |
``` |
| 60 |
|
| 61 |
With `baseURL = https://example.org/docs/` |
| 62 |
|
| 63 |
```go-html-template |
| 64 |
{{ relURL "/" }} → / |
| 65 |
{{ relURL "/articles" }} → /articles |
| 66 |
{{ relURL "/style.css" }} → /style.css |
| 67 |
``` |
| 68 |
|
| 69 |
> [!NOTE] |
| 70 |
> As illustrated by the previous example, using a leading slash is rarely desirable and can lead to unexpected outcomes. In nearly all cases, omit the leading slash. |
| 71 |
|
| 72 |
[`urls.RelLangURL`]: /functions/urls/rellangurl/ |