Skip to content

Commit fad0cee

Browse files
committed
redirect_to/aliases: keep the anchor, if any was specified
When redirecting from a URL specifying an anchor (e.g. links like https://git-scm.com/docs/git#_git_commands), the regular redirection mechanism would lose the `#<anchor>` part, which is unfortunate. This is important because some translated manual pages point to non-existing translated pages with an anchor. For example, the German version of the `git` manual page points to `docs/user-manual/de#git-concepts`, i.e. to the `git-concepts` anchor of the German version of the `user-manual` (that does not exist yet). Redirecting to such a large page without that specific anchor would be quite unhelpful for the reader. So let's make sure that redirections respect the specified anchor (this requires Javascript code to be executed, in case Javascript is disabled this will fall back to the traditional "redirect-without-anchor" functionality). We do this in the layout that handles our custom `redirect_to:` front matter directive as well as in the newly-introduced `layouts/alias.html` which overrides Hugo's default handling of the `aliases:` directive. One easy to miss part of this patch is that `layouts/alias.html` must be careful to URI-encode any question mark in the URL to redirect to: These are not URI-encoded in Hugo's `.Permalink` (at least not when the page itself specifies a `url` in its front matter that contains a bare question mark, something we need to do in the book sections because otherwise the code implementing the navigation faces problems). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 6e69b7f commit fad0cee

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

layouts/_default/baseof.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<meta name="robots" content="noindex">
1414
</head>
1515
<body>
16-
<script>location="{{ relURL .Params.redirect_to }}"</script>
16+
<script>window.location.replace(document.querySelector("link[rel='canonical']").href + window.location.hash)</script>
1717
<h1>Redirecting&hellip;</h1>
1818
<a href="{{ $redirect_to }}">Click here if you are not redirected.</a>
1919
</body>

layouts/alias.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{ $redirect_to := replaceRE `\.html$` "" (replace .Permalink "?" "%3F") }}
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Redirecting&hellip;</title>
6+
<link rel="canonical" href="{{ $redirect_to }}">
7+
<meta http-equiv="refresh" content="0; url={{ $redirect_to }}">
8+
<meta name="robots" content="noindex">
9+
</head>
10+
<body>
11+
<script>window.location.replace(document.querySelector("link[rel='canonical']").href + window.location.hash)</script>
12+
<h1>Redirecting&hellip;</h1>
13+
<a href="{{ $redirect_to }}">Click here if you are not redirected.</a>
14+
</body>
15+
</html>
16+

0 commit comments

Comments
 (0)