Relative URLs, Redirects and Pagination

Are relativeURLs, redirects and pagination fundamentally at odds? I can’t seem to configure my site such that both work consistently. Possibly related to #2199 Investigate RelativeURLs=true and aliases

Let’s say I have relativeURLs enabled for the scenarios below.

I create a page with an alias and I do not have baseURL set. The generated alias doesn’t seem to bring along the relativeURL goodness:

Frontmatter

---
title: Jobs in the Southeast
url: /work-with-us
aliases:
    - /jobs
---

Generated alias page

<!DOCTYPE html>
<html>
  <head>
    <title>/work-with-us/</title>
    <!-- should be ../work-with-us/ -->
    <link rel="canonical" href="/work-with-us/"/>
    <meta name="robots" content="noindex">
    <meta charset="utf-8" />
    <meta http-equiv="refresh" content="0; url=/work-with-us/" />
  </head>
</html>

Adding baseURL

With baseURL=https://www.fws.gov/southeast I can get the aliases to work as expected, but then my pagination links break as it starts to mash together baseURL and relative paths.

<!-- redirect works as expected -->
<meta http-equiv="refresh" content="0; url=https://www.fws.gov/southeast/work-with-us/" />

<!-- should be ../articles/ -->
<a href="../southeast/southeast/articles/">1</a>

Customizing alias.html

I figured I could just customize my alias.html page in order to get around this. Unfortunately I can’t access the .Site or $.Site variables to access a site parameter.

<link rel="canonical" href="{{ .Site.Params.base }}{{ .Permalink }}" />

Error: Error building site: template: alias.html:6:36: executing "alias.html" at <$.Site.Params.base>: can't evaluate field Site in type struct { Permalink string; Page *hugolib.Page }

What about your other templates (for pagination), and config? Do you have a minimal example repo to reproduce this? It seems like you’d fix this in the pagination template, ne?

I need to set up a minimalist version of my repo to share here. The repo on GitHub is currently 7GB and I don’t expect folks to download to help me out.

I seem to have found a solution as you suggested. I reintroduced a baseURL variable in my site config and used {{ .URL | absURL }} to generate the pagination links. The baseURL variable seems to have been used to generate the aliases, too.

Pagination

  <li>
    <a href="https://www.fws.gov/southeast/articles/page/2/" aria-label="Next" class="pagination-next">
      <span aria-hidden="true">&raquo;</span>
    </a>
  </li>

Alias (/jobs)

<!DOCTYPE html>
<html>
  <head>
    <title>https://www.fws.gov/southeast/work-with-us/</title>
    <link rel="canonical" href="https://www.fws.gov/southeast/work-with-us/"/>
    <meta name="robots" content="noindex">
    <meta charset="utf-8" />
    <meta http-equiv="refresh" content="0;url=https://www.fws.gov/southeast/work-with-us/" />
  </head>
</html>
1 Like