Add <html lang=""> to META redirect pages (layout\alias.html)

I have raised feature request #9586 on github regarding adding language reference on Meta redirect pages.

Following @jmooring response in other thread I tried to add custom layout layouts\alias.html as follow

<!DOCTYPE html>
<html lang="{{ .Site.LanguageCode }}">
    <head>
        <title>{{ .Permalink }}</title>
        <link rel="canonical" href="{{ .Permalink }}"/>
        <meta name="robots" content="noindex">
        <meta charset="utf-8" />
        <meta http-equiv="refresh" content="0; url={{ .Permalink }}" />
    </head>
</html>

However this result following error

execute of template failed: template: alias.html:2:20: executing "alias.html" at <.Site.LanguageCode>: error calling Site: runtime error: invalid memory address or nil pointer dereference

Any idea how to get this working, or is that a bug?

I am unable to reproduce the problem. Please post your site configuration.

I somehow thought I added a pull request to that whole template, but I can’t find it, so it’s probably something that never happened :wink: But nonetheless, here it is:

The template should be only:

<!DOCTYPE html>
<meta http-equiv="refresh" content="0; url={{ .Permalink }}" />

Because:

  • if it’s HTML5 then no HTML tag and HEAD tag are needed
  • lang attributes make no sense if the only function of the page is to redirect to another page. There is no content!
  • title tag is obsolete if the page is shown for 0 seconds
  • canonical tag is useless because a meta-refresh is a 301 redirect which means the page is never indexed
  • robots tags are useless because a meta-refresh is a 301 redirect which means the page is never indexed
  • charset is useless for a page without content.

The default alias template of hugo should be just those two lines in my sample above. all else is overkill.

Also: It’s a redirect. Make the content of that request as short/little as possible so the browser does not have to load so much data. I know, 2kb is not much, but 0.5kb is much less.

1 Like

And really, if your HTML checker is complaining about your redirect files, then just configure it to ignore redirect files. This is not an HTML page per see, it’s a simple file that makes the browser redirect to the proper page. There is no content, so why would you want to define the language of this “not content”.

Hi @jmooring
Thank you for your offer to look at this.
I have added you on GitHub site to the repo as a Collaborator.

Interesting fact I found that in config.toml I am using languageCode and in code I am asking for LanguageCode.

Despite that this doesn’t cause any issue in head.html, on this file when I change it to small first letter I got the following error

layouts/alias.html:2:15": execute of template failed: template: alias.html:2:15: executing "alias.html" at <.languageCode>: can't evaluate field languageCode in type hugolib.aliasPage

Without going in deep to other aspects (ie. canonical), meta-refresh is not classed as 301 redirect, at least not according to ahrefs.com. 301 redirects are server side. Aliases in hugo are client-side. If I want 301 I am using _redirects for example (on Netlify), but I want to have working aliases as well.

Ref canonical, the meta redirect serve a purpose for users to point to other pages, but canonical URL serve search engines to give some feedback, ignoring the fact if they (i.e. Google) will use this data or not). In that instance “less is more” may not serve its purpose.

What exactly is the content whose language you try to define in your HTML tag? I am asking only because you are opening a PR to change HTML that serves more than just your own page. And a redirect, which is the core message in my response, has no content that has a language. So adding attributes just because your HTML validator program is complaining about it seems to be the wrong reaction.

@idarek

You are paginating in layouts/partials/taxonomy-li.html.

When you paginate, an alias is created at (for example) public/tags/page/1/index.html.

This uses the same alias template, and within that context, neither .Site nor .Page.Site are available. Use the site function instead.

<!DOCTYPE html>
<html lang="{{ site.LanguageCode }}">
    <head>
        <title>{{ .Permalink }}</title>
        <link rel="canonical" href="{{ .Permalink }}"/>
        <meta name="robots" content="noindex">
        <meta charset="utf-8" />
        <meta http-equiv="refresh" content="0; url={{ .Permalink }}" />
    </head>
</html>
1 Like

Interesting.
Didn’t know the use of site.. Your help is as always on top-notch!

ps. is there anything in Hugo documentation to describe use scenarios for site instead .Site?

I am interested in couple aspects

Could you back this up as even Google example of meta redirect contain tag.

  • if it’s HTML5 then no HTML tag and HEAD tag are needed

Re this, that probably make sense. I was thinking about experimenting with meta redirect and when I found this post I finally decided to do this, and adding lang to href was one of implementation of custom meta redirect that I want to try.

  • lang attributes make no sense if the only function of the page is to redirect to another page. There is no content!

From above Google website. This is why canonical may not be used all time, but Google algorithms may need that.
"While your users generally won’t be able to tell the difference between the different types of redirects, Google Search uses redirects as a strong or weak signal that the redirect target should be canonical. "

  • canonical tag is useless because a meta-refresh is a 301 redirect which means the page is never indexed

As mentioned earlier, 301 are server side, meta redirects are client, browser. Depend whatw possible and available, despite that this is least recommended method, it still quite usefull and this is the way how aliases are working in Hugo.

  • robots tags are useless because a meta-refresh is a 301 redirect which means the page is never indexed

From ahrefs.com: “Although Google might not be using the HTML lang attribute today, other search engines and browsers do.” In most scenarios this is not needed, but better to have than don’t…

  • charset is useless for a page without content.

The site function is documented here:
https://gohugo.io/functions/site/

I use it everywhere due to laziness/convenience. I don’t need to think about the root context of the template, or the current context (inside a with or range block).

When writing examples, sometimes I intentionally avoid using it because I want the reader to become familiar with the concept of context, which is really slick when you learn how to leverage it to your advantage.

2 Likes

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