diff options
Diffstat (limited to 'docs/content/en/functions/absurl.md')
-rw-r--r-- | docs/content/en/functions/absurl.md | 75 |
1 files changed, 43 insertions, 32 deletions
diff --git a/docs/content/en/functions/absurl.md b/docs/content/en/functions/absurl.md index 13524d2d6..efea2df75 100644 --- a/docs/content/en/functions/absurl.md +++ b/docs/content/en/functions/absurl.md @@ -1,50 +1,61 @@ --- title: absURL -description: Creates an absolute URL based on the configured baseURL. -date: 2017-02-01 -publishdate: 2017-02-01 -lastmod: 2017-02-01 +description: Returns an absolute URL. categories: [functions] menu: docs: - parent: "functions" + parent: functions keywords: [urls] signature: ["absURL INPUT"] -workson: [] -hugoversion: -relatedfuncs: [relURL] -deprecated: false -aliases: [] --- -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/`: +With multilingual configurations, use the [`absLangURL`] function instead. The URL returned by this function depends on: +- Whether the input begins with a slash +- The `baseURL` in site configuration + +### Input does not begin with a slash + +If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`. + +With `baseURL = https://example.org/` + +```go-html-template +{{ absURL "" }} → https://example.org/ +{{ absURL "articles" }} → https://example.org/articles +{{ absURL "style.css" }} → https://example.org/style.css ``` -{{ "mystyle.css" | absURL }} → "https://example.com/hugo/mystyle.css" -{{ "mystyle.css" | relURL }} → "/hugo/mystyle.css" -{{ "http://gohugo.io/" | relURL }} → "http://gohugo.io/" -{{ "http://gohugo.io/" | absURL }} → "http://gohugo.io/" + +With `baseURL = https://example.org/docs/` + +```go-html-template +{{ absURL "" }} → https://example.org/docs/ +{{ absURL "articles" }} → https://example.org/docs/articles +{{ absURL "style.css" }} → https://example.org/docs/style.css ``` -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 (SEO)][jsonld], where some of your images for a piece of content may or may not be hosted locally: +### Input begins with a slash -{{< code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" >}} -<script type="application/ld+json"> -{ - "@context" : "http://schema.org", - "@type" : "BlogPosting", - "image" : {{ apply .Params.images "absURL" "." }} -} -</script> -{{< /code >}} +If the input begins with a slash, the resulting URL will be incorrect when the `baseURL` includes a subdirectory. With a leading slash, the function returns a URL relative to the protocol+host section of the `baseURL`. -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. +With `baseURL = https://example.org/` + +```go-html-template +{{ absURL "/" }} → https://example.org/ +{{ absURL "/articles" }} → https://example.org/articles +{{ absURL "/style.css" }} → https://example.org/style.css +``` + +With `baseURL = https://example.org/docs/` + +```go-html-template +{{ absURL "/" }} → https://example.org/ +{{ absURL "/articles" }} → https://example.org/articles +{{ absURL "/style.css" }} → https://example.org/style.css +``` -{{% note "Ending Slash" %}} -`absURL` and `relURL` are smart about missing slashes, but they will *not* add a closing slash to a URL if it is not present. +{{% note %}} +The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function. {{% /note %}} -[apply function]: /functions/apply/ -[configuration]: /getting-started/configuration/ -[jsonld]: https://developers.google.com/search/docs/guides/intro-structured-data -[safejs]: /functions/safejs +[`absLangURL`]: /functions/abslangurl/ |