Multiple URLs to page depending on taxonomy (related content different)


#1

Hi,

I’m trying to figure out how I should solve this problem. I’m using Hugo v0.29.

The basic idea is this:
“If visitor accesses a page from a category, I want the URL to be based on that category, so I can show similar pages for that category”.

This does break the idea of non-duplicate content, but, I would then use .Permalink as the link rel="canonical" (ref: hxxps://support.google.com/webmasters/answer/139066?hl=en) to specify that the post, independently of what tag you accessed it from, has one real link that is the correct permalink.

At the same time, I wanted to remove the word categories from the URL, by just having the category directly.

I’ve been able to get to this point:

[permalinks]
category = "/:filename"

(this puts all categories in the root, without the keyword category)

Then I set:

pagecat: "shirts"

In category/shirts/index.md

Then, on the category page, I loop out the pages like this:

		{{ $pagecat := .Params.pagecat }}
		{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
			{{ if eq $name $pagecat }}
				{{ range $taxonomy.Pages }}
				<li><a href="{{ "" | relLangURL }}{{ $name | urlize }}/{{ .Params.Slug }}">{{ .Title }} {{ .Params.Price }}</a></li>
				{{ end }}
			{{ end }}
		{{ end }}

(This is using the technique explained here: hxxps://novelist.xyz/tech/custom-urls-for-category-pages-in-hugo/ )

As you see above, I’m not using the .Permalink, but instead building up the URL myself using the taxonomy name and the .Slug.

However, here’s the problem, I’m not able to regenerate versions for the same page under each taxonomy category so I can loop different items related to that category. I had an idea to hack this using alias.html that was implemented a while ago (hxxps://discourse.gohugo.io/t/customize-alias-page/4242/4) but the problem is that the alias.html-file is not specific to a section, and also it does not seem to be able to access the Page-attributes any more:

alias.html:

Test: {{ .Page.Params.Title }}

Gives:

Error: Error building site: template: alias.html:1:14: executing "alias.html" at <.Page.Params.title>: can't evaluate field Params in type *hugolib.Page

When buildling. Also, it seems like no test is actually verifying that the Page.Params can actually be accessed: https://github.com/gohugoio/hugo/blob/v0.29/hugolib/alias_test.go#L41 so this might actually be a bug that wasn’t spotted by the tests.

If alias.html would work, I could use that one as the non-Permalink-version of the page, having the link rel="canonical" header but the same content as the real page, which would solve this issue. The only problem then would be that the alias.html is not section dependent, but site-wide, but that might be something that would be possible to solve (maybe even I could do it).

So, question:
Is there any good way to solve the problem with different related information on the article, depending on from what taxonomy category you accessed it with by making copies of the page but being rendered for different taxonomies depending on the article? (I rather not fix this with javascript, and duplicate content is not an issue by using the link rel="canonical")

To understand my issue, I’ve attached a test-project (https://www.dropbox.com/s/b5u5iyqm4hdzeaz/test-project.zip?dl=1) that won’t even start due to the alias-error. When removing the {{ .Page.Params.Title }}in alias.html you will get it to build.

Would love to know how others have solved the issue with “different related content for the same article in different categories” before.

(Sorry for the garbled links, but new users wasn’t allowed to post more than two links in a post)

Regards,
Frans


#2

Your post has a lot to unpack, so how about we step back and you explain a little more about the end results. You describe:

I personally don’t know what this looks like. Do you know of a site that does it, or can you describe how this looks with a few pages in an example?


#3

Sorry for the wall of text, @maiki, I’ll try to explain better:

Let’s say I have articles for men and women, some articles will be posted for both, but accessing the article from the women-section, should still only show articles in the women section.

What I want to achieve is basically:

Listing articles for women:

https://example.com/women/

Listing articles for men:

https://example.com/men/

An article posted to both categories should show up as:

https://example.com/women/article1
https://example.com/men/article1

And depending on the category it was generated in, I would be able to loop the same taxonomy for more articles:

(watching the article from /women/article1 would list these ones)
https://example.com/women/article2
https://example.com/women/article3

(in /men/article1 it would list these ones)
https://example.com/men/article4
https://example.com/men/article5

As I mentioned, I would use .Permalink inside link rel=canonical to prevent crawlers to think I had duplicate content. alias.html would solve this, but as mentioned, doesn’t seem to be able to successfully read the .Page.Params of the page it’s redirecting to when customizing the alias-page.

Hope I was more clear this time, sorry for the confusion.


#4

Yeah, that is a bit clearer. I’m sorry to say, I won’t be assisting with this. I don’t think that is how the canonical link element is supposed to be used, and having site infrastructure like that isn’t good for machines or people.

Good luck on figuring out the alias.html parameters issue. Since you already have that template created, maybe add {{ printf "%#v" . }} to it, via template debugging. :slight_smile:


#5

Thanks, no worries, I understand it’s a bit of a weird pattern, however, the idea was basically to be able to present content differently in different sections, even though the original content was the same.

The debugging actually says the Page-parameter is in the alias-template, but Hugo won’t compile if you refer to any properties of it:

Thanks anyway for a quick reply :slight_smile:


#6

I’ve never had a reason to change the alias template, and I doubt many others have either, so the way it works could have changes and no one would notice. That is a little out of my league, so maybe someone that knows that can assist.

But sorry, why are you creating aliases? Are you removing the meta refresh, and instead generating an entire page at that location?