# String replace after .RelPermalink in href is evaluated

**URL:** <https://discourse.gohugo.io/t/string-replace-after-relpermalink-in-href-is-evaluated/55027>\
**Category:** support\
**Created:** [June 10, 2025, 2:56pm UTC](https://discourse.gohugo.io/t/string-replace-after-relpermalink-in-href-is-evaluated/55027 "2025-06-10T14:56:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zannas88](https://avatars.discourse-cdn.com/v4/letter/z/e79b87/32.png) [@zannas88](https://discourse.gohugo.io/u/zannas88)\
**Post date:** [June 10, 2025, 2:56pm UTC](https://discourse.gohugo.io/t/string-replace-after-relpermalink-in-href-is-evaluated/55027/1 "2025-06-10T14:56:25Z")

</div>

Good evening, I’m having some trouble due to unresolved problem linked here:

> <https://github.com/gohugoio/hugo/issues/8734>
>
> \### What version of Hugo are you using (\`hugo version\`)?
> 
> \<pre\>
> $ hugo versio…n
> hugo v0.85.0-724D5DB5+extended windows/amd64 BuildDate=2021-07-05T10:46:28Z VendorInfo=gohugoio
> \</pre\>
> 
> \### Notable settings
> \<pre\>
> relativeURLs = true
> \</pre\>
> 
> \### Does this issue reproduce with the latest release?
> 
> \*\*YES\*\*
> 
> \### Description
> 
> Hugo seems to generate different links for the exact same template depending on the context it is used. Given this:
> \`\`\`
> {{- range .Pages -}}
> \<a href="{{.RelPermalink}}"\>{{.RelPermalink}}\</a\>\<br\>
> {{ end -}}
> \`\`\`
> And its output:
> !\[Output\](https://i.imgur.com/OyNuW5z.png) 
> 
> You can clearly see that Hugo is only applying relative transformations (../) to links within src/href. However, my understanding is that this should happen everywhere. Specifically, I'm using it as something akin to the following (this is vastly oversimplified but illustrates my usecase):
> \`\`\`
> \<script\>
> urls = \[
> {{- range .Pages -}}
> {{.RelPermalink}},
> {{ end -}}
> \]
> for (key in document.getElementsByClassName("needs-links")) {
> document.getElementsByClassName("needs-links")\[key\].href = urls\[key\];
> }
> \</script\>
> \`\`\`
> As you may guess, the RelPermalinks here will not be transformed into the dots (../../), and thus the links will be wrong depending on the website it is deployed to.
> 
> I've tried \`{{ .RelPermalink | relURL }}\` to no avail.

My goal is just to have “n” website generated with hugo. They are with same theme, same configuration etc…the differences will be only the logo and the contents.

So, I’d like to “reuse” the same CSS, FONTS and JS directories.  
To do that, I have setup Hugo in order to have the following directories:  
-CommonServices (with inside the CSS, FONTS and JS)  
-Site1  
-Site2  
-…  
-SiteN

I try to “modify” the theme in “partial/myfile.html”…but I found this problem.

```auto
{{- with resources.Get "/css/file.css" }}
	<link href="{{ .RelPermalink }}{{ $assetBusting }}" rel="stylesheet"></link>
{{- end }}

```

What I want to do is easily this:

```auto
{{- with resources.Get "/css/file.css" }}
	<link href="{{ replace .RelPermalink "/css" "../CommonServices/css" }}{{ $assetBusting }}" rel="stylesheet"></link>
{{- end }}

```

Due the problem metioned in the [link](https://github.com/gohugoio/hugo/issues/8734) I have not the right result and the only way is to write a script in some language that read all html files and do the substituion of

```auto
foreach fileHTML in Site
   originalHTML = read(fileHTML)
   newHTML = originalHTML.replace("/css","../CommonServices/css")
   fileHTML = save(newHTML)

```

Instead of write this “script”, is possible to do that in goHugo bypassing the “bug”?

Thanks

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 10, 2025, 4:29pm UTC](https://discourse.gohugo.io/t/string-replace-after-relpermalink-in-href-is-evaluated/55027/2 "2025-06-10T16:29:12Z")

</div>

It would be helpful to see the project repository. For example, are you mounting the common directory to assets in each site’s config?

---

<div class="post-metadata">

**Author:** ![zannas88](https://avatars.discourse-cdn.com/v4/letter/z/e79b87/32.png) [@zannas88](https://discourse.gohugo.io/u/zannas88)\
**Post date:** [June 11, 2025, 7:38am UTC](https://discourse.gohugo.io/t/string-replace-after-relpermalink-in-href-is-evaluated/55027/3 "2025-06-11T07:38:49Z")

</div>

> [@jmooring](#):
>
> For example, are you mounting the common directory to assets in each site’s config?

Good morning, thanks for the reply.  
I have one directory called CommonSevices, where I have put manually all the common files once.

Then I have several Hugo Projects (= websites). When I want to publish I run  
`hugo -d myDirectory` for a single website. In the script for the “publication” I then delete the directory “JS, Fonts and CSS” because I want to use the files inside the “CommonServices” that is outside the publication directory of each hugo project.  
As I organized, my goal is just to add “../CommonServices” in front of “/js”, “/css”, “/font”.  
To do that, I need that hugo give me the string .RelPermalink as it was evaluated inside an href attribute and then I can proceed with the replace operation.

Actually I can’t do that because when I write `replace .RelPermaLink "/js" "../CommonServices/js"` .RelPermaLink is not evaluated as it was in a href attribute, but as it was outside.

I also try this code but does not work (the relative characters `..` is lost):

Example:

```auto
{{$x := string (print `href=` .RelPermalink) }}
{{ $x }} <<--- show: `href=../fonts/etc`
{{ replace $x `href=` `` }} <<-- show: `/font/etc` (the `..` is lost :tired_face: )

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 11, 2025, 4:16pm UTC](https://discourse.gohugo.io/t/string-replace-after-relpermalink-in-href-is-evaluated/55027/4 "2025-06-11T16:16:46Z")

</div>

It seems like we’re trying to solve the wrong problem here. Your approach in an anti-pattern.

Typically you would place the shared _source_ files in their own directory, then mount that directory to assets. Or use environments. Or create 4 repositories: 1 for the shared theme/config, and the others for each site.

---

<div class="post-metadata">

**Author:** ![zannas88](https://avatars.discourse-cdn.com/v4/letter/z/e79b87/32.png) [@zannas88](https://discourse.gohugo.io/u/zannas88)\
**Post date:** [June 12, 2025, 8:24am UTC](https://discourse.gohugo.io/t/string-replace-after-relpermalink-in-href-is-evaluated/55027/5 "2025-06-12T08:24:27Z")

</div>

Thank u so much for the reply. Can give me a very basic and simple axample in order to better understand please?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 12, 2025, 2:26pm UTC](https://discourse.gohugo.io/t/string-replace-after-relpermalink-in-href-is-evaluated/55027/6 "2025-06-12T14:26:21Z")

</div>

> [@zannas88](#):
>
> Can give me a very basic and simple example

Sure, but it depends on what you actually want to do:

1. Do you want to publish three sites, each with their own URL?
2. Do you want all three sites in one repository, or can they be separated?
