Redirect post to external URL

I am new with Hugo and trying to redirect to an external link (Medium page) after clicking a post header:

Therefore I added canonicalUrl to the post front matter.

title: "Analysing 260.000 Text Documents (1/5)"
featured_image: "/images/project-6/collecting_data.png"
date: 2023-04-28
draft: False
tags: ["Python", "SQL", "NLP", "Machine Learning"]
canonicalUrl: ""

But it still opens the description page, instead of opening the medium website.

I am using template Ananke and my Github site is: Hello World | Gunardi
My guess is I need to modify the following file layout/post/summary.html :

1  <div class="mb3 pa4 {{ $.Param "text_color" | default "mid-gray" }} overflow-hidden">
2   {{ if .Date }}
3      <div class="f6">
4        {{ .Date | time.Format (default "January 2, 2006" .Site.Params.date_format) }}
5      </div>
6    {{ end }}
7    <h1 class="f3 near-black">
8      {{ $href := .RelPermalink }}
9      {{ with resources.Get .Params.relPermalinkOverride }}
10        {{ $href = .RelPermalink }}
11      {{ end }}
      <a href="{{ $href }}" class="link black dim">
        {{ .Title }}
    <div class="nested-links f5 lh-copy nested-copy-line-height">
      {{ .Summary  }}

I suppose the code from line 8-11 need to be changed.
I want the click on post header to behave like this:
a. if a canonicalURL (or externalURL) is present, open this URL directly,
b. else if relPermalinkOverride is available, open the link of this parameter,
c. else open the normal description site for the post.

At the moment, the point b and c are working (line 8-11 above). I just want to add point a.

Following pages might be helpful, still I cannot really implement it:
a. Feature Request: Add front-matter variable for redirecting article to external URL · Issue #5621 · gohugoio/hugo · GitHub
b. How can I redirect to an external link on click of header of any blog? - #5 by RickCogley
c. Adding an External Canonical URL to a Hugo Template

Thank you in advance to any help, Gunardi

Didn’t we already cover this?

Yeah, just half way there. Because the solution from that post could only open HTML file which is stored under folder assets. Now I want to open an external URL instead of opening HTML file from assets.

Just modify the code you changed last time, and use the same front matter param.

git clone --single-branch -b hugo-forum-topic-44042 hugo-forum-topic-44042
cd hugo-forum-topic-44042
hugo server
1 Like

Thank you Jmooring for your assistance. Really appreciate it… Your solution is even better than my initial suggestion with “canonicalUrl”. :slight_smile:

1 Like

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