Preserve the #some anchor

Redirection can be implement server-side or client-side.

Hugo’s aliases is client-side, creating an HTML page with an meta http-equiv="refresh"using this template. You could override this template by creating layouts/alias.html, and add some JS to capture and append the query string and fragment. Something like:

<!DOCTYPE html>
<html{{ with site.LanguageCode | default site.Language.Lang }} lang="{{ . }}"{{ end }}>
  <head>
    <title>{{ .Permalink }}</title>
    <link rel="canonical" href="{{ .Permalink }}">
    <meta name="robots" content="noindex">
    <meta charset="utf-8">
    <script>
      window.location = "{{ .Permalink }}" + window.location.search + window.location.hash;
    </script>
  </head>
</html>

However, whenever possible, use server-side redirection instead. The “how” of this depends your hosting setup.

1 Like