relurl.md (1876B)
1 ---
2 title: relURL
3 description: Creates a baseURL-relative URL.
4 date: 2017-02-01
5 publishdate: 2017-02-01
6 categories: [functions]
7 menu:
8 docs:
9 parent: "functions"
10 keywords: [urls]
11 signature: ["relURL INPUT"]
12 workson: []
13 hugoversion:
14 relatedfuncs: [absURL]
15 deprecated: false
16 aliases: []
17 ---
18
19 Both `absURL` and `relURL` consider the configured value of `baseURL` in your site's [`config` file][configuration]. Given a `baseURL` set to `https://example.com/hugo/`:
20
21 ```
22 {{ "mystyle.css" | absURL }} → "https://example.com/hugo/mystyle.css"
23 {{ "mystyle.css" | relURL }} → "/hugo/mystyle.css"
24 {{ "http://gohugo.io/" | relURL }} → "http://gohugo.io/"
25 {{ "http://gohugo.io/" | absURL }} → "http://gohugo.io/"
26 ```
27
28 The last two examples may look strange but can be very useful. For example, the following shows how to use `absURL` in [JSON-LD structured data for SEO][jsonld] where some of your images for a piece of content may or may not be hosted locally:
29
30 {{< code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" >}}
31 <script type="application/ld+json">
32 {
33 "@context" : "https://schema.org",
34 "@type" : "BlogPosting",
35 "image" : {{ apply .Params.images "absURL" "." }}
36 }
37 </script>
38 {{< /code >}}
39
40 The above uses the [apply function][] and also exposes how the Go template parser JSON-encodes objects inside `<script>` tags. See [the safeJS template function][safejs] for examples of how to tell Hugo not to escape strings inside of such tags.
41
42 {{% note "Ending Slash" %}}
43 `absURL` and `relURL` are smart about missing slashes, but they will *not* add a closing slash to a URL if it is not present.
44 {{% /note %}}
45
46 [apply function]: /functions/apply/
47 [configuration]: /getting-started/configuration/
48 [jsonld]: https://developers.google.com/search/docs/advanced/structured-data/intro-structured-data
49 [safejs]: /functions/safejs