# URLencode in Hugo

**URL:** https://discourse.gohugo.io/t/urlencode-in-hugo/24215
**Category:** support
**Created:** [March 24, 2020, 12:06pm UTC](https://discourse.gohugo.io/t/urlencode-in-hugo/24215 "2020-03-24T12:06:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![chop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chop/32/10718_2.png) [@chop](https://discourse.gohugo.io/u/chop)
#### Post date: [March 24, 2020, 12:06pm UTC](https://discourse.gohugo.io/t/urlencode-in-hugo/24215/1 "2020-03-24T12:06:39Z")

</div>

Hi! I’d like to create share buttons without tracking scripts and cookies in my template, but to do that, I need to urlencode my Permalink.

What I mean by “urlencode” is the transformation [this online tool](https://www.urlencoder.org/) operates.

For instance, I could create a Facebook button by adding a link to `http://www.facebook.com/sharer/sharer.php?u={{ .Permalink | urlencodeThisSomehow }}`

I tried looking at [what Go offers for URLs](https://godoc.org/net/url). [PathEscape](https://godoc.org/net/url#PathEscape) seems to be the equivalent, but it does not seem to be available in Hugo templates.

I suppose I _could_ create a partial to do this by combining regex, [urls.Parse](https://gohugo.io/functions/urls.parse/) and [Go’s URL.EscapedPath](https://godoc.org/net/url#URL.EscapedPath), but this looks quite complicated indeed for this operation.

Do you know of a proper way to do this?

---

<div class="post-metadata">

### Author: ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)
#### Post date: [March 24, 2020, 6:19pm UTC](https://discourse.gohugo.io/t/urlencode-in-hugo/24215/2 "2020-03-24T18:19:09Z")

</div>

You might be able to use [https://gohugo.io/functions/htmlescape/](https://gohugo.io/functions/htmlescape/).

---

<div class="post-metadata">

### Author: ![chop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chop/32/10718_2.png) [@chop](https://discourse.gohugo.io/u/chop)
#### Post date: [March 24, 2020, 7:36pm UTC](https://discourse.gohugo.io/t/urlencode-in-hugo/24215/3 "2020-03-24T19:36:25Z")

</div>

Sadly no. HtmlEscape is used to display special characters can be displayed in an HTML page, while URLencode/PathEscape escapes special characters so that they can be used in a URL. The encoding rules are not the same.

---

<div class="post-metadata">

### Author: ![Jake1](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jake1](https://discourse.gohugo.io/u/Jake1)
#### Post date: [March 29, 2020, 9:32pm UTC](https://discourse.gohugo.io/t/urlencode-in-hugo/24215/4 "2020-03-29T21:32:01Z")

</div>

In my share partial:  
\<a href=“[https://www.facebook.com/sharer/sharer.php?u=](https://www.facebook.com/sharer/sharer.php?u=){{ .Permalink }}”  
\<a href=“[https://twitter.com/intent/tweet?url=](https://twitter.com/intent/tweet?url=){{ .Permalink }}&text={{ .Title }}&hashtags={{ .Params.hash }}&via={{ .Site.Params.twitter.username }}”

If you look at source code of page on web, this renders as: (changed less than bracket to dots - would not show)  
… a title=“Share on Facebook” class=“link dib h2 w2 mr3” href=“[https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.Domain.com%2Fsubdir%2Fpage.htm](https://www.facebook.com/sharer/sharer.php?u=https%3a%2f%2fwww.Domain.com%2fsubdir%2fpage.htm)” target="\_blank" rel=“noopener” …

as you can see, this is urlencoded. And it works, share to fb, twitter and email.

Note on AMP version of site, use amp-social-share  
…amp-social-share class=“mr3 dim” type=“facebook” data-param-app\_id=“259967834022664” width=“42” height=“42” data-param-href="{{ .Permalink }}"\>\</amp-social-share…

---

<div class="post-metadata">

### Author: ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)
#### Post date: [April 3, 2020, 7:57pm UTC](https://discourse.gohugo.io/t/urlencode-in-hugo/24215/5 "2020-04-03T19:57:14Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
