Parse a URL in Hugo

Published: Jan 25, 2019
Updated: Jan 14, 2022

Need to parse a URL in your Hugo templates? Thanks to the urls.Parse function, you don’t have to do a bunch of string manipulation. Here’s a quick example.

We’ll start with this code:

{{ $url := "https://zwbetz.com/make-a-hugo-blog-from-scratch/#wrap-up" }}
{{ $url = urls.Parse $url }}

The urls.Parse function returns a URL struct, so the value of each struct field would be:

Field Value
$url.Scheme https
$url.Host zwbetz.com
$url.Path /make-a-hugo-blog-from-scratch/
$url.Fragment wrap-up